不知何故,零宽度否定后视断言 - 如果前面有\则不匹配 ") - 无法按预期工作。
这是我现在拥有的:
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\")"]]
最后的匹配是错误的。
最佳答案
您的文本不包含任何反斜杠(\
)
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\\\")"]]
关于ruby - 零宽度否定后视断言无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330112/