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 after numbers before double underscore

I would like to extract substrings that start after a number and before a double underscore. Below you see two test strings and the expected output after ->.

'1234514 TEST STRING__blabla3452b' -> 'TEST STRING'
'16275653 TEST_STRING__bl67abla3452b' -> 'TEST_STRING'

The regex I came up with so far: (?:^|\n)\d+([^__]+) only returns the first but not the second output as the second underscore is not recognised. I tried to escape the underscores but that did not work. Any help would be very much apprecciated.

Thanks.

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

>Solution :

You can use

^\d+\s*(.*?)(?=__)
\d\s+(.*?)(?=__)

See the regex demo #1 and regex demo #2.

Details:

  • ^ – start of string
  • \d+ – one or more digits
  • \s* – zero or more whitespaces
  • (.*?) – Group 1: any zero or more chars other than line break chars as few as possible
  • (?=__) – a location immediately followed with __.
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