simple pattern matching question

Bengt Richter bokr at oz.net
Thu Jun 6 10:50:01 EDT 2002


On Wed, 05 Jun 2002 20:18:04 -0700 (PDT), "Sean 'Shaleh' Perry" <shalehperry at attbi.com> wrote:

>
>On 06-Jun-2002 Corey G. wrote:
>> Gurus,
>> 
>> How would I match with an item from a list?  I would imagine something
>> this simple would not require a regular expression.
>> 
>> web_servers=('www01','www02','www03')
>> 
>>         for server_match in web_servers:
>>            if (trap == "Service:"server_match "State:down"):
>>             print "found match"
>> 
>> The comparsion should be: if trap == Service:www01 State:down
>> 
>> server_match prints "as is, but not from the list".  I tried many 
>> combinations but decided to leave the most obviously wrong one.
>> 
>
>if trap == ("Service:%s State:down" % server_match):
>  print "found match"
>
That should work, if you know that the trap string will never
have an extra blank, or tab instead of space, or something additional
in it, or change case or order.

An alternative to building a match to the entire trap string
is to extract and normalize the parts of interest, according
to the syntax of trap. The syntax will determine if your best
solution is a regular expression, or maybe just .lower().split()
etc. Put a comment in your code referencing where the trap
syntax is defined/determined/guaranteed ;-)

Notice what kinds of changes you'll need if someone later
adds " TimeStamp:..." or such to the trap string, at the front
or back.

Regards,
Bengt Richter



More information about the Python-list mailing list