【发布时间】:2018-05-07 15:00:47
【问题描述】:
ex = "g4net:HostName=abc}\n Unhandled Exception: \nSystem.NullReferenceException: Object reference not set to an";
puts ex[/Unhandled Exception:(.*?):/,0]
/Unhandled Exception:(.*?):/ 应该与 \nSystem.NullReferenceException 匹配(在 rubular 中测试)但它一直没有显示任何结果。
我是红宝石新手。请帮助我如何从给定的字符串中提取 /Unhandled Exception:(.*?):/ 的匹配项
【问题讨论】:
-
Rubular 从字面上解释您的
\n。您必须单独插入行以获得相同的行为:rubular.com/r/MOdKAd2Xnl。提示:.仅在设置了m标志时才匹配换行符。 -
如果你想匹配文本,请使用正向lookbehind:
puts ex[/(?<=Unhandled Exception:).*?:/m]。