为什么这个正则表达式在Rubular中起作用,而在Python中不起作用?

时间:2013-06-02 23:27:43

标签: python regex

我正在尝试使用正则表达式来解析SHOUTcast Total Time Spent Listening report

示例字符串:

#1         6,916,236.75      32,000,555        4,587,363         COOLfahrenheit 93
#2         4,457,026.25      3,328,957         1,168,349         idobi Radio: New. Music. Unfiltered. [url redacted]

正则表达式:

^\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$

Rubular下,返回:

Match 1
1.  6,916,236.75
2.  32,000,555
3.  4,587,363
4.  COOLfahrenheit 93
Match 2
1.  4,457,026.25
2.  3,328,957
3.  1,168,349
4.  idobi Radio: New. Music. Unfiltered. [URL redacted]

但是,当与Python的re.search()一起使用时,会返回None。我做错了什么?

1 个答案:

答案 0 :(得分:6)

在没有看到代码的情况下无法确定,但我猜你忘了使用多行标志,re.MULTILINE

Ruby的独特之处在于^ / $ 始终匹配行的开头/结尾。在其他常见的风格中,除非使用m标志,否则它们不会。