我尝试了几个在 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'"]]
我预计match
会返回类似的结果,scan
但事实并非如此。谁能解释为什么?