Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text - Regular Expression for search and replace

Tags:

I want to use search & replace with a regular expression to turn the following text:

"some text i dont care about \cite[p. 6]{some-bibtex-citation} some more text I care even less about \cite[p. 321 / 4]{and-another-citation-from-bibxtex} and still more text."

into this:

"some text i dont care about [\citem{author}{some-bibtex-citation}, p. 6] some more text I care even less about [\citem{author}{and-another-citation-from-bibxtex}, p. 321 / 4] and still more text."

As you can see, all of the \cite occurences have been replaced with a different one. They are now surrounded by [ and ] and they all start with [citem{author}

I tried it with the following search regex which basically works but can select too much text all at once:

\\cite\[(.+)\]\{(.+)\} 

The replace regex I used for this is:

[\\citem{author}{\2}, \1] 
like image 551
user35938 Avatar asked Sep 02 '13 13:09

user35938


1 Answers

Change your regex so that the .+ becomes lazy:

\\cite\[(.+?)\]\{(.+?)\}            ^        ^ 

Making a greedy quantifier lazy means that instead of matching as much as possible, it will match as little as possible. In your current regex, the . will match all the characters until it finds another ) (followed by the other characters in the regex).

Or use negated classes:

\\cite\[([^\]]+)\]\{([^}]+)\} 
like image 132
Jerry Avatar answered Oct 20 '22 20:10

Jerry