简体   繁体   English

匹配未在JAVA中转义的引号的正则表达式

[英]Regular expression that matches quotes that are not escaped in JAVA

I am trying to create a regex in JAVA that would match a string like: 'test " abcd \\" ef" test' Let's say that I would want to know if between the quotes I have the characters abcdef in this order and any other character between them (but since I'm interested only in the substring between the quotes, the character between them can't be a quote, except the case in which the quote is escaped) Is it possible to do this? 我正在尝试在JAVA中创建一个正则表达式,该正则表达式将匹配以下字符串:'test“ abcd \\” ef“ test'假设我想知道引号之间的字符abcdef是否以此顺序和其他任何字符它们之间(但由于我只对引号之间的子字符串感兴趣,因此,除引号被转义的情况外,它们之间的字符不能是引号)可以这样做吗?

I managed to create this regex 我设法创建了这个正则表达式

("[^\"]*\"[^\"]*a[^\"]*b[^\"]*c[^\"]*d[^\"]*e[^\"]*f[^\"]*\"[^\"]*")

that works for any case except the ones with escaped quotes embedded in the string. 适用于任何情况,但字符串中包含转义引号的除外。

You're almost there... add the case for the quoted quote, which can be matched with 您快到了...添加引号的大小写,可以将其与

\\\"

so each of your [^\\"]* cases (except the first and last, I guess) should become 因此,您的每个[^\\"]*案例(我想第一个和最后一个除外)都应变为

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

... but you also need to take care of backslashes (because, for example, in ...,但您还需要注意反斜杠(例如,

"foo\\"

the final quote is a "real" (non-escaped) quote, even though there's a backslash before it.) So in fact you need the [^\\"]* cases to become: 最后一个引号是一个“真实”(不转义)引号,即使它前面有一个反斜杠。)因此,实际上您需要[^\\"]*大小写成为:

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

or in other words: match anything that's not \\ or " , or is \\ followed by a character that's ignored. 或换句话说:匹配所有非\\" ,或\\后面跟一个被忽略的字符。

NB This will mean that, for example, in the string "xxx\\abcdef" the "\\a" will not be matched as an "a" , but that's probably what you want (since "\\a" typically denotes the ASCII "BEL" control character). 注意:这意味着,例如,在字符串"xxx\\abcdef""\\a"将不与"a"匹配,但这可能就是您想要的(因为"\\a"通常表示ASCII“ BEL” “控制字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM