1

Suppose I want to search for things like

\todo{English sentence}

Then the regex

\\todo{\_.\{-}}

works (I don't know why. I copied it from somewhere long time ago (embarrassed)).

But suppose I want to match things like

\todo{anything whatsoever}

Then the above regex is no good.

For example, if I have

\todo{The problem is caused because of {another} pair of braces inside}

The previous regex just matches

\todo{The problem is caused because of {another}

What is the correct regex, and if possible, can someone break it down a little bit? Things like \_.\{-} already seem to make no sense!

Big thanks.

  • 2
    Regexes aren't meant to match recursive patterns. There exists hacks that do so, but in the end they are still hacks that recursively inject (up to a point before the slow down is too important) ways to workaround embedded pairs of brackets. You'll have better luck if you select up to the { then, jump to } with % -- as long as you can still apply the action you wish. – Luc Hermitte Jul 17 '18 at 16:12
3

Per the help pages in Vim:

\{-}    matches 0 or more of the preceding atom, as few as possible
*       Matches 0 or more of the preceding atom, as many as possible.

In my small test case replacing the former with the latter matched your example string. That is:

\\todo{\_.*}
  • It was working for me at first. But after the text became more complicated, a problem has appeared. The regex you have suggested is also matching \\todo{stuff} as long as there is a closing brace} – caffeinemachine Jul 17 '18 at 17:31
1

As @LucHermite already mentioned in a comment, regular expressions are not meant for recursive patterns. The solution by @Vitor does not work, as was already confirmed in the comment, because it will match beyond the last } of the \todo command if possible.

Since you did not write anything about what your end goal is, it is difficult to give further concrete tips here. But I would think that you could solve your initial problem e.g. with something like this:

  1. Search for \\todo

  2. Do vf{ to select up until the first {

  3. Do % to increase the selection up until the closing }

If you need some sort of repeated action, you can combine these things with a macro, e.g. qa/\\todo<cr>vf{% ... q where ... is the action you want to perform. Then you can run the macro with @a to repeat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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