简体   繁体   English

Java正则表达式将带引号的字符串与嵌入式转义的引号匹配

[英]Java regex to match quoted string with embedded escaped quote

I need help with a regex for matching a quoted string that could have an embedded escaped quote. 我需要正则表达式的帮助,以匹配可能包含嵌入式转义引号的带引号的字符串。

For example, given string "'John\\'s bike'" . 例如,给定字符串"'John\\'s bike'" The basic pattern i was starting with, "'[^']*'" , and expanding it to negate case of embedded escaped quotes using lookbehind doesn't work: "'((?<\\\\)[^'])*'" . 我最初使用的基本模式是"'[^']*'" ,并将其扩展为使用lookbehind否定嵌入式转义引号的大小写不起作用: "'((?<\\\\)[^'])*'"

Anyone has idea? 有人知道吗?

The string could be a series of quoted strings, eg, "'John\\'s bike', 'Mary\\'s hat', 'my shoes'", and i want to tokenize each of the quoted substrings separately. 该字符串可以是一系列带引号的字符串,例如“'John \\'s bike','Mary \\'s hat','my shoes'”,我想分别标记每个带引号的子字符串。

You can accept any character between quotes which is 您可以接受引号之间的任何字符,即

  • preceded by \\ 前面有\\
  • not ' . 不是'

So your regex can look like: 因此您的正则表达式可能如下所示:

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

which in Java should look like "'(\\\\\\\\.|[^'])*'" (to create \\ literal in regex we need to escape it like \\\\ but same rules applies in String literals so we need to escape these two \\ again which finally gives us "\\\\\\\\" ) 在Java中看起来应该像"'(\\\\\\\\.|[^'])*'" (要在正则表达式中创建\\文字,我们需要像\\\\一样对其进行转义,但是在String文字中应用相同的规则,因此我们需要进行转义这两个\\再次给我们"\\\\\\\\"

Demo based on your example: 根据您的示例进行演示:

String input = "'John\\'s bike', 'Mary\\'s hat', 'my shoes'";
Pattern p = Pattern.compile("'(\\\\.|[^'])*'");
Matcher m = p.matcher(input);
while(m.find()){
    System.out.println(m.group());
}

Output: 输出:

'John\'s bike'
'Mary\'s hat'
'my shoes'

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

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