Sergey Alikin
Sergey Alikin

Reputation: 161

Regex for matching groups without end delimiter

I have a string:

@# Text2@#  Line1@#Last line without delimiter

So each element is started with @# and finished with @#, but last element has only start delimiter. and I would like to have three (in this case, but number is not fixed) matched groups:
Text2,
Line1,
Last line without delimiter

I did this regex:

(@#(.*))

And it's working, but only when each element located on separated line (.* is limited with LF char).
Also it's very preferable to do not use modern regex features, because regex engine is boost 1.34 (legacy code).

Upvotes: 1

Views: 39

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626754

You can use

@#(.*?)(?=@#|$)

Details:

  • @# - delimiter
  • (.*?) - Capturing group 1: any 0+ chars other than line break chars as few as possible (to match any chars, it might suffice to add (?s) at the pattern start, or use [\w\W] instead of . here)
  • (?=@#|$) - up to the first @# or end of string.

Upvotes: 1

Related Questions