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:


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

My attempted solution is:

for i in x:
    result ='\n(.*)(', i)

This returns the error ‘unterminated subpattern’.

>Solution :

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

  • \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

Leave a Reply