如何将双引号字符串与转义的双引号字符匹配? [英] How can I match double-quoted strings with escaped double-quote characters?

查看:48
本文介绍了如何将双引号字符串与转义的双引号字符匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 Perl 正则表达式来匹配一个字符串.我假设只有双引号字符串," 是文字引号字符而不是字符串的结尾,并且 是文字反斜杠字符,不应转义引号字符.如果不清楚,一些示例:

I need a Perl regular expression to match a string. I'm assuming only double-quoted strings, that a " is a literal quote character and NOT the end of the string, and that a is a literal backslash character and should not escape a quote character. If it's not clear, some examples:

"""    # string is 1 character long, contains dobule quote
"\"    # string is 1 character long, contains backslash
"\""  # string is 2 characters long, contains backslash and double quote
"\\"  # string is 2 characters long, contains two backslashes

我需要一个正则表达式,它可以将所有 4 种可能性以及这些可能性的所有其他简单变体都识别为有效字符串.我现在拥有的是:

I need a regular expression that can recognize all 4 of these possibilities, and all other simple variations on those possibilities, as valid strings. What I have now is:

/".*[^\]"/

但那是不对的 - 除了第一个之外,它不会匹配任何一个.任何人都可以就如何处理这个问题给我一个正确的方向吗?

But that's not right - it won't match any of those except the first one. Can anyone give me a push in the right direction on how to handle this?

推荐答案

这个怎么样?

/"([^\"]|\\|\")*"/

匹配零个或多个不是斜杠或引号或两个斜杠或一个斜杠然后一个引号的字符

matches zero or more characters that aren't slashes or quotes OR two slashes OR a slash then a quote

这篇关于如何将双引号字符串与转义的双引号字符匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆