Sublime Forum

How to learn regular expression in sublime?

#1

I want to replace some text in sublime. I have some text {{abcd dog cat}}, and I want to make them become {abcd dog cat}. I cannot directly replace {{ to { because some {{ is necessary in my text. I know using regular expression can achieve my purpose. But I don’t know how to use it? Can anyone give me some recommended book or tutorial?

0 Likes

#2

You could have a look to : https://www.regular-expressions.info/tutorial.html , I go to this site when I forgot some advanced syntax, but the tutorial looked ok to me. And except for named group, you can very easily test regexp in sublime directly.

As for your precise stuff the regexp to search would be {{(.*?)}} abd the replace would be {\1}
Quick explanation:

  • the . means any character
  • the * means repeated n times with n starting at 0
  • the ? means non-greedy search, i.e. it will stop at the first occurence of }}. If you do not put it will continue until it find the last }}
  • The parenthesis around the .*? is just to group it and to be able to reference it later
  • the \1 in the replace expression is a reference to the first group in search regexp. If you had multiple group ()()() you could reference those with \1, \2 and \3
1 Like