regular expression question

Fredrik Lundh fredrik at pythonware.com
Tue Feb 15 02:14:58 EST 2005


Bruno Desthuilliers wrote:

>> match = STX + '(.*)' + ETX
>>
>> # Example 1
>> # This appears to work, but I'm not sure if the '+' is being used in
>> the regular expression, or if it's just joining STX, '(.*)', and ETX.
>>
>> if re.search(STX + '(.*)' + ETX,data):
>>   print "Matches"
>>
>> # Example 2
>> # This also appears to work
>> if re.search(match,data):
>>   print "Matches"

> You may want something like:
> if re.search('%s(.*)%s' % (STX, ETX), data):
>   ...

that's of course the same thing as examples 1 and 2.

a tip to the original poster: if you're not sure what an expression does,
try printing the result.  use "print repr(v)" if the value may contain odd
characters.  try adding this to your test script:

    print repr(match)
    print repr(STX + '(.*)' + ETX)
    print repr('%s(.*)%s' % (STX, ETX))

</F> 






More information about the Python-list mailing list