Ruby 零宽度负查找后断言无法按预期工作

Ruby 零宽度负查找后断言无法按预期工作,ruby,regex,Ruby,Regex,不知何故,如果前面有\,则零宽度负查找后面断言-不匹配“)不起作用 这就是我现在拥有的: text = <<EOT h1=_("This is a test") h2=_("This is a test \"quotes\"") h3=_("This is a test (\"quotes\")") EOT text.scan(/_\("(.*?)(?<!\\)"\)/) => [["This is a test"], ["This is a test \"quote

不知何故,如果前面有\,则零宽度负查找后面断言-不匹配“)不起作用

这就是我现在拥有的:

text = <<EOT
h1=_("This is a test")
h2=_("This is a test \"quotes\"")
h3=_("This is a test (\"quotes\")")
EOT

text.scan(/_\("(.*?)(?<!\\)"\)/)

=> [["This is a test"], ["This is a test \"quotes\""], ["This is a test (\"quotes"]]

最后一个匹配错误。

您的文本不包含任何反斜杠(
\

转义反斜杠或使用以下形式:

text = <<'EOT'
h1=_("This is a test")
h2=_("This is a test \"quotes\"")
h3=_("This is a test (\"quotes\")")
EOT
puts text
p text.scan(/_\("(.*?)(?<!\\)"\)/)
text = <<EOT
h1=_("This is a test")
h2=_("This is a test \"quotes\"")
h3=_("This is a test (\"quotes\")")
EOT
puts text
text = <<'EOT'
h1=_("This is a test")
h2=_("This is a test \"quotes\"")
h3=_("This is a test (\"quotes\")")
EOT
puts text
p text.scan(/_\("(.*?)(?<!\\)"\)/)