Sunday, August 25, 2019

regex - Regular expression search replace in Sublime Text 2



I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so something like converting this text:



Hello my name is bob



And this search term:



Find what: my name is (\w)+




Replace with: my name used to be $(1)



The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.


Answer



Usually a back-reference is either $1 or \1 (backslash one) for the first capture group (the first match of a pattern in parentheses), and indeed Sublime supports both syntaxes. So try:



my name used to be \1



or



my name used to be $1


Also note that your original capture pattern:



my name is (\w)+



is incorrect and will only capture the final letter of the name rather than the whole name. You should use the following pattern to capture all of the letters of the name:



my name is (\w+)

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...