刚从正则表达式开始......我正在寻找一个
像 \b\d\d\b 这样的正则表达式,但数字可能不一样。(例如 23 应该匹配
但 22 不应该)我已经尝试了很多(涉及反向引用),但他们都失败了。
我已经使用下面的代码(python 2.7.3)尝试了 RE,但到目前为止没有任何匹配项
import re
# accept a raw string(e) as input
# and return a function with an argument
# 'string' which returns a re.Match object
# on succes. Else it returns None
def myMatch(e):
RegexObj= re.compile(e)
return RegexObj.match
menu= raw_input
expr= "expression\n:>"
Quit= 'q'
NewExpression= 'r'
str2match= "string to match\n:>"
validate= myMatch(menu(expr))
# exits when the user # hits 'q'
while True:
# set the string to match or hit 'q' or 'r'
option = menu(str2match)
if option== Quit: break
#invokes when the user hits 'r'
#setting the new expression
elif option== NewExpression:
validate= myMatch(menu(expr))
continue
reMatchObject= validate(option)
# we have a match !
if reMatchObject:
print "Pattern: ",reMatchObject.re.pattern
print "group(0): ",reMatchObject.group()
print "groups: ",reMatchObject.groups()
else:
print "No match found "