[Tutor] List of regular expressions

Alan G alan.gauld at freenet.co.uk
Wed Jun 22 09:39:38 CEST 2005


> for pattern in thelist:
>    regex=re.compile(pattern)
>    if regex.match('24110'):
>        the_pattern = pattern
>        .
>        .
>        sys.exit(0)
>
> but in this case it will pick thelist[2] and not the list[3] as I
wanted to,
> how can I have it pick the pattern that describes it better from the
list.

Define 'better'.
Both regex describe it equally well, how is Python supposed to
know which one is 'better'? You have to tell it by ordering your
list according to your idea of better, usually meaning most
specific test first.

Alan G



More information about the Tutor mailing list