Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex match double ## including any repetition of # whih is not double

How to match everything after a double hash "##" until the next double hash "##" and including any repetition of the "#" character which is not "##".
For instance the below example should return two matches, one for chapter 1 and 1.1 and the second for chapter 2.

## chapter 1

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Suspendisse mollis magna nec felis gravida, id posuere libero molestie.

### subchapter 1.1

Sed vel ipsum eget tortor maximus ultrices vitae eget dolor.

## chapter 2

Aenean pellentesque lectus quis ex tristique ultrices. Vestibulum eget purus eu ipsum vestibulum pulvinar

At the moment the best I found is the following regex:

((?!#){2}[\s\S])+

which however is confused when a ### or #### is found and is counted as a new chapter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Link to regex example: https://regex101.com/r/gydtq1/1

>Solution :

You can use

re.findall(r'(?ms)^##(?!#).*?(?=\n##(?!#)|\Z)', text)
re.findall(r'^##(?!#).*?(?=\n##(?!#)|\Z)', text, re.M | re.S)

See the regex demo. Details:

  • (?ms) – a re.DOTALL (re.S) and re.MULTILINE (re.M) flags
  • ^ – start of a line
  • ##(?!#) – a ## string not immediately followed with a #
  • .*? – zero or more chars as few as possible
  • (?=\n##(?!#)|\Z) – a location immediately followed with a newline and ## not immediately followed with a # or end of string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading