regular expression question

S

snacktime

The primary question is how do I perform a match when the regular
expression contains string variables? For example, in the following
code I want to match a line that starts with STX, then has any number
of characters, then ends with STX.
Example 2 I'm pretty sure works as I expect, but I'm not sure about
Example 1, and I'm pretty sure about example 3.

import re
from curses.ascii import STX,ETX,FS
STX = chr(STX)
ETX = chr(ETX)
FS = chr(FS)
data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX
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"

# Example 3
# Doesn't work, as STX and ETX are evaluated as the literal strings
'STX' and 'ETX'
if re.search('STX(.*)ETX', data):
print "Matches"

Chris
 
B

Bruno Desthuilliers

snacktime a écrit :
The primary question is how do I perform a match when the regular
expression contains string variables? For example, in the following
code I want to match a line that starts with STX, then has any number
of characters, then ends with STX.
Example 2 I'm pretty sure works as I expect, but I'm not sure about
Example 1, and I'm pretty sure about example 3.

import re
from curses.ascii import STX,ETX,FS
STX = chr(STX)
ETX = chr(ETX)
FS = chr(FS)
data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX
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"

# Example 3
# Doesn't work, as STX and ETX are evaluated as the literal strings
'STX' and 'ETX'
if re.search('STX(.*)ETX', data):
print "Matches"

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

BTW, given your requirements, I'd write this:
if re.search('^%s(.*)%s$' % (STX, ETX), data):
...
 
F

Fredrik Lundh

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,120
Members
47,741
Latest member
WilliamsFo

Latest Threads

Top