4

I use visual-regexp to make query regexp search. Consider this text:

enter image description here \enquote{\Large{text1}} \enquote{\Large{text2}} \enquote{\Large{text3}} \enquote{\Large{text4}}

I want regexp to match every and single macro group (the 4 groups). I invoke a query regexp by C-c q and entered this regexp:

\(\\Large{\)\(.?+\)\(}\)\(}\)

Problem is that it returned one match instead of 4 matches:
enter image description here

the \(.?+\) was very greedy to match all 4 macro groups.

But, when I get each of the 4 groups in its own line I get the desired effect:
enter image description here

Now I could do replace with: \2\4 to get the desired result:
enter image description here

So what I was missing when all were on the same line?

Plus, can you please recommend any good documentation on using regexp/grep in Emacs?

2
  • 1
    You should use .+? and not .?+.. Jan 6, 2015 at 12:44
  • 1
    Yep. that solved it! feel free to upgrade your comment to an accepted answer.
    – doctorate
    Jan 6, 2015 at 12:47

2 Answers 2

6

you could use non greedy + like \(\\Large{\)\(.+?\)\(}\)\(}\) (note the inversion of the ? and the +) if you add a ? after a + the + will match the smaller match possible. Note also that .?+ is the same as .*

4

You want to match "Large, opening brace, anything but a closing brace, the closing brace". This is the corresponding regexp

"\\\\Large{[^}]*}"

Note the regexp would be more complicated if the argument of \Large has groups (i.e. braces) itself. TeXnical note: \Large is a declaration, must not be used as a command with arguments.

re-builder is very handy to practice regexp searches.

Regarding the documentation, the starting point is the Elisp manual: https://www.gnu.org/software/emacs/manual/html_node/elisp/Regular-Expressions.html#Regular-Expressions (or (info "(elisp)Regular expressions") in Emacs)

3
  • @doctorate There are different syntaxes for regexps, some with more `s, some with less. re-builder` is just to write a regexp, not to search & replace (I haven't said that). Which group do you want to replace? The argument of \Large?
    – giordano
    Jan 6, 2015 at 12:11
  • I updated the question to make it more clear.
    – doctorate
    Jan 6, 2015 at 12:34
  • "\\\\Large{.+?}" is a shorter version though.
    – doctorate
    Jan 6, 2015 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.