2

不知何故,零宽度否定后视断言 - 不匹配 ") 如果前面有 \ - 不能按预期工作。

这就是我现在所拥有的:

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"]]

但结果应该是:

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

最后一场比赛是错误的。

4

1 回答 1

1

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

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

印刷

h1=_("This is a test")
h2=_("This is a test "quotes"")
h3=_("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(/_\("(.*?)(?<!\\)"\)/)

印刷

h1=_("This is a test")
h2=_("This is a test \"quotes\"")
h3=_("This is a test (\"quotes\")")
[["This is a test"], ["This is a test \\\"quotes\\\""], ["This is a test (\\\"quotes\\\")"]]
于 2013-08-20T08:25:21.423 回答