Python Regex Capture text between matches

Brand new for regex and trouble matching.

I have the following line:

AGM will be held at the Company's registered office in Unity House, Telford Road, Basingstoke, Hampshire, RG21 6YJ on January 13, 2016 at 10.00.

The company announces that its 2016 annual general meeting will be held on February 11, 2016 at 10.00 in Hangar 89, London, Luton Airport, Luton, Bedfordshire, LU2 9PF.

I am trying to extract the address from the last occurrence of 'at' to the zip code. Thus, Unity House, Telford Road, Basingstoke, Hampshire, RG21 6YJ and Hangar 89, London Luton Airport, Luton, Bedfordshire, LU2 9PF

This is what I use (at)(?!.*at)(.*)\s([A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}) it only retrieves the second address. Any thoughts?

thanks

+4
source share
1 answer

It looks like you want to use ((?:(?!at).)*)instead (?!.*at)(.*)to not skipat

(at)((?:(?!at).)*)\s([A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2})

See demo at regex101

If you are using the (at)(?!.*at)(.*)s flag, then only the last is atnot the other atin front. Therefore, it is expected that only the latter will be consistent. (at)((?:(?!at).)*)will not miss another at.

+2
source

Source: https://habr.com/ru/post/1626899/


All Articles