Home > Enterprise >  REGEX_String between strings in a list
REGEX_String between strings in a list

Time:05-14

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 '('.

My attempted solution is:

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

This returns the error 'unterminated subpattern'. Thankyou

CodePudding user response:

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