Sunday 25 August 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...