为什么我的正则表达式找不到结果?

时间:2011-11-08 23:57:55

标签: python regex

数据是这样的:

post date='2008-7' postCount='8' viewCount='24308' questionCount='5' answerCount='76' answeredQuestion='5'

这是我的任务代码:

p1 = re.compile(r"post date")
m1 = p1.search(postline) 
if m1:
    month = re.search(r'post date="([\d\W]+)"', postline).group(1)

2 个答案:

答案 0 :(得分:4)

您正在搜索双qoutes ",而您的示例是单qoutes '

re.search(r'post date="([\d\W]+)"'
                      ^         ^

post date='2008-7' postCount='8' viewCount='24308' questionCount='5' answerCount='76' answeredQuestion='5'
          ^      ^

编辑:问题不再出现在正则表达式中,而是你的python。这是一个例子:

import re
postline = "post date='2008-7' postCount='8' viewCount='24308' questionCount='5' answerCount='76' answeredQuestion='5'"
pattern = "post date='([\d\W]+)'"

p1 = re.compile(pattern)
date = p1.search(postline).group(1) 
print date

输出:

2008-7

答案 1 :(得分:0)

您的约会时间是否格式为YYYY-M?如果是这样,相应地构建您的模式?此模式允许YYYY-MYYYY-MM

>>> import re
>>> postline = "post date='2008-7' postCount='8' viewCount='24308' questionCount='5' answerCount='76' answeredQuestion='5'"
>>> pat = r"post date='(\d{4}-\d{1,2})'"
>>> re.search(pat, postline).group(1)
'2008-7'