正则表达式,re.compile,返回None而不是匹配

时间:2014-01-30 20:18:53

标签: python xml regex

我正在尝试在Python中编译正则表达式。 以下字段采用pdml(基于XML)格式。

showname: Origin-Host:0005-diamproxy.WSBOMAGJPNC.Gx.vzims.com

我的正则表达式是:

re.compile (showname="Origin-Host: ([^"]+))")

当我尝试搜索模式时,它会在输出中给我None。我认为我的正则表达式有问题。

正则表达式有什么问题,我应该如何解决?

1 个答案:

答案 0 :(得分:1)

试试这个:

r = re.compile('showname: Origin-Host:(.+)')

它适用于示例输入:

s = 'showname: Origin-Host:0005-diamproxy.WSBOMAGJPNC.Gx.vzims.com'
r.match(s).group(0) 
=> 'showname: Origin-Host:0005-diamproxy.WSBOMAGJPNC.Gx.vzims.com'
r.match(s).group(1)
=> '0005-diamproxy.WSBOMAGJPNC.Gx.vzims.com'

问题中的代码存在引号问题,请注意compile()接收字符串作为参数。

相关问题