0

I have following text

# Heading Level 1\\r\\n## Heading Level 2\\r\\n### Heading 
Level 3\\r\\n#### Heading Level
4\\r\\n##### Heading Level 5\\r\\n###### Heading Level 6\\r\\nHeading Level 13
Alternate\\r\\n======================\\r\\n

I need to match Heading Level 13 Alternate (basically anything b/w newline and ===

I have tried following

(?<=\\\\r\\\\n)?.*?(?=\\\\r\\\\n=+)

But the problem is that my regex is being greedy with \r\n and is going all the way back. I want to stop it at first \r\n

java_doctor_101
  • 3,287
  • 4
  • 46
  • 78

1 Answers1

0

You can use

(?<=\\\\r\\\\n)(?:(?!\\\\r\\\\n)[\w\W])*?(?=\\\\r\\\\n=)

See the regex demo. Details:

  • (?<=\\\\r\\\\n) - a location immediately preceded with \\r\\n string
  • (?:(?!\\\\r\\\\n)[\w\W])*? - (a tempered greedy token) any char, zero or more but as few as possible occurrences, that is not the starting point for a \\r\\n char sequence
  • (?=\\\\r\\\\n=) - a location immediately followed with \\r\\n= string.

Here, [\w\W] is used instead of . in order to match across lines (. does not match line break chars by default, but that depends on the regex flavor). You might also use s flag or its (?s) inline version (in Onigmo/Ruby, replace s with m) with a dot.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563