Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个正则表达式可以在Rubular中工作,而不能在Python中工作?_Python_Regex - Fatal编程技术网

为什么这个正则表达式可以在Rubular中工作,而不能在Python中工作?

为什么这个正则表达式可以在Rubular中工作,而不能在Python中工作?,python,regex,Python,Regex,我正在尝试使用正则表达式来解析 示例字符串: #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+

我正在尝试使用正则表达式来解析

示例字符串:

#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+(.+)$
在下,这将返回:

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一起使用时,将返回
None
。我做错了什么?

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


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

您是如何使用它的?记住使用原始字符串:
r“^\S+\S+(\S+)\S+(\S+)\S+(\S+)$”
就是这样。谢谢另外,我应该使用
find_all()
。换句话说,Ruby是唯一一种保持regex原始行为不受
sed
和POSIX影响的语言。其他语言(我相信是从Perl开始的)发明了一种不必要的“多行”模式,而不仅仅是使用
\A
\Z
:P