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_String between strings in a list

From this list:

['AUSTRALIA\nBELMONT PARK (WA', '\nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (QLD']

I would like to reduce it to this list:

['BELMONT PARK', 'EAGLE FARM']

You can see from the first list that the desired words are between '\n' and '('.

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

My attempted solution is:

for i in x:
    result = re.search('\n(.*)(', i)
    print(result.group(1))

This returns the error ‘unterminated subpattern’.
Thankyou

>Solution :

You’re getting an error because the ( is unescaped. Regardless, it will not work, as you’ll get the following matches:

  • \nBELMONT PARK (
  • \nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (

You can try the following:

(?<=\\n)(?!.*\\n)(.*)(?= \()
  • (?<=\\n): Positive lookbehind to ensure \n is before match
  • (?!.*\\n): Negative lookahead to ensure no further \n is included
  • (.*): Your match
  • (?= \(): Positive lookahead to ensure ( is after match
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