2

I'm having an problem to return the matches in separated groups, since the last match ")" only works in the last register. I want to return all three matches in the example below. Any advice?

Link to regex101.

regex = "\[\]\(((?s).*)\)"


text = " ![](https://tomticket-
         anexos.s3.amazonaws.com/inimg/imagename1.jpg)

         ![](https://tomticket-
         anexos.s3.amazonaws.com/inimg/imagename2.jpg)

         ![](https://tomticket-
         anexos.s3.amazonaws.com/inimg/imagename2.jpg)
       "

everything after the first ![]( and last ) is returned.

  • It might help if you mention which language you are using. Or add an appropriate language tag (just the language you're using, please). – rici Sep 18 '19 at 19:52

2 Answers2

2

This happens because .* is greedy. It will consume as much characters as it can, including any closed parentheses it may find along the way.

To fix this problem, use [^)]* to tell regex that you are explicitly excluding the ) character from the list of characters that you want to capture.

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Your pattern \[\]\(((?s).*)\) will first match []( and then uses an inline modifier (?s) which will make the dot match a newline so .* will match till the end and will then backtrack until the last )

You could use a negated character class instead matching from the opening parenthesis ( until the first closing parenthesis ).

!\[\]\([^)]+\)
  • ! Match literally if you also want to match that
  • \[\] Match []
  • \( Match (
  • [^)]+ Match 1+ times any char except ) which will also match a newline
  • \) Match )

regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70