Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Ruby 为什么match和scan对同一个正则表达式返回不同的结果?_Ruby_Regex - Fatal编程技术网

Ruby 为什么match和scan对同一个正则表达式返回不同的结果?

Ruby 为什么match和scan对同一个正则表达式返回不同的结果?,ruby,regex,Ruby,Regex,我在Ruby中尝试了几种匹配选项,结果让我感到困惑,因为match和scan返回不同的结果。这是一个例子: s ="C:\Users\rdo\AppData\Local\Temp/ccqZSpeQ.o: In function `main':\r\nmain.c:(.text+0x8): undefined reference to `some_function'" %r[([\w\.\/]+):(.+)].match(s) # => #<MatchData "C:Users\rd

我在Ruby中尝试了几种匹配选项,结果让我感到困惑,因为
match
scan
返回不同的结果。这是一个例子:

s ="C:\Users\rdo\AppData\Local\Temp/ccqZSpeQ.o: In function `main':\r\nmain.c:(.text+0x8): undefined reference to `some_function'"

%r[([\w\.\/]+):(.+)].match(s)
# => #<MatchData "C:Users\rdoAppDataLocalTemp/ccqZSpeQ.o: In function `main':\r" 1:"C" 2:"Users\rdoAppDataLocalTemp/ccqZSpeQ.o: In function `main':\r">

s.scan(/([\w\.\/]+):(.+)/)
# => [["C", "Users\rdoAppDataLocalTemp/ccqZSpeQ.o: In function `main':\r"], ["main.c", "(.text+0x8): undefined reference to `some_function'"]]
s=“C:\Users\rdo\AppData\Local\Temp/ccqZSpeQ.o:在函数'main'中:\r\n main.C:(.text+0x8):对'some_函数'的未定义引用”
%r[([\w\.\/]+):(.+)].match(s)
# => #
s、 扫描(/([\w\.\/]+):(.+)/)
#=>[[“C”,“Users\rdoAppDataLocalTemp/ccqZSpeQ.o:在函数'main':\r“],[“main.C”,“(.text+0x8):对'some_函数'的未定义引用]]

我期望
match
会返回类似
scan
的结果,但它没有返回。有人能解释为什么吗?

match
scan
是完全不同的方法,它们做完全不同的事情,返回完全不同的结果

match
告诉您将正则表达式应用于字符串的结果、匹配的内容、它们的开始和长度以及模式的哪个部分匹配


scan
将正则表达式应用于字符串并返回一个字符串数组所有捕获的结果。

这里有什么混淆?它们是不同的方法,做不同的事情,返回不同的东西。文档中很清楚每个部分的作用。实际上,我不明白为什么
match
只匹配一个部分或表达式?php中的
match
preg\u match
的一个类似物吗?