正则表达式匹配一个忽略\“的字符串

时间:2014-04-23 21:55:40

标签: regex dart

我目前有这个正则表达式

"[^"]*"

我正在测试它的字符串(我正在使用http://regexpal.com/所以它还没有进行字符串编码!)

"This is a test \"Text File\"" "This is a test \"Text File\""

目前正在匹配

"This is a test \"
""
"This is a test \"
""

我希望它有以下匹配

"This is a test \"Text File\""
"This is a test \"Text File\""

基本上我希望它匹配以“以...开头”的东西,但忽略中间的任何东西是“。”我需要添加到我的正则表达式来实现这一点吗?

提前致谢

3 个答案:

答案 0 :(得分:1)

然后,最好的方法取决于你的正则表达式引擎的匹配功能(其中许多都有各种功能的不同支持)。对于不支持任何类型的后视功能的简单正则表达式引擎,这就是您想要的:"([^"]*\\")*[^"]*"

这将匹配一个引号,然后是零对或多对非引用序列和\"序列,后跟一个必需的非引用序列,最后是最终引用。

答案 1 :(得分:1)

(\\"|[^"])+

将匹配\"以及任何非"

的字符

答案 2 :(得分:0)

DART的正则表达式:

RegExp exp = new RegExp(r"(".*?"")");

http://regex101.com/r/hM5pI7

<强>说明

Match the regular expression below and capture its match into backreference number 1 «(".*?"")»
   Match the character “"” literally «"»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match the characters “""” literally «""»