Search code examples
pythonregexsearchgroup

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

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