[Tutor] using re groups

Kent Johnson kent37 at tds.net
Fri Mar 6 15:10:18 CET 2009


On Fri, Mar 6, 2009 at 7:56 AM, ski <norman at khine.net> wrote:
> Hello,
> I have this:
>
>>>> import re
>>>> s = "Association of British Travel Agents (ABTA) No. 56542\nAir Travel
>>>> Organisation Licence (ATOL)\nAppointed Agents of IATA (IATA)\nIncentive
>>>> Travel & Meet. Association (ITMA)"
>>>> licenses = re.split("\n+", s)
>>>> licenseRe = re.compile(r'\(([A-Z]+)\)( No. (\d+))?')
>>>> for license in licenses:
> ...     m = licenseRe.search(license)
> ...     print m.group(1, 3)
> ...
> ('ABTA', '56542')
> ('ATOL', None)
> ('IATA', None)
> ('ITMA', None)
>
> What is the best way to also extract the affiliation name i.e:
>
> 'Association of British Travel Agents',
> 'Air Travel Organisation Licence'

I would add another group to the beginning of the re that matches
everything before the first (, e.g.
r'([^(]+)\(([A-Z]+)\)( No. (\d+))?'

Kent


More information about the Tutor mailing list