1

我的文件包含一些行,例如

"This is a string." = "This is a string's content."
" Another \" example \"" = " New example."
"My string
can have several lines." = "My string can have several lines."

我需要提取子字符串:

This is a string.
This is a string's content.
 Another \" example \"
 New example.
My string
can have several lines.
My string can have several lines.

这是我的代码:

String regex = "\".*?\"\\s*?=\\s*?\".*?\"";
Pattern pattern = Pattern.compile(regex,Pattern.DOTALL);
Matcher matcher = pattern.matcher(file);

目前,我可以得到“=”的左右部分。但是当我的子字符串包含“\””时,我的正则表达式就不能正确地完成工作。

任何人都可以帮我写正确的正则表达式吗?我尝试了 \"^[\\"] 而不是 \",但它没有用..

提前谢谢。

4

3 回答 3

3
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile(
    "\"          # Match a quote\n" +
    "(           # Capture in group number 1:\n" +
    " (?:        # Match either...\n" +
    "  \\\\.     # an escaped character\n" +
    " |          # or\n" +
    "  [^\"\\\\] # any character except quotes or backslashes\n" +
    " )*         # Repeat as needed\n" +
    ")           # End of capturing group\n" +
    "\"          # Match a quote", 
    Pattern.COMMENTS);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group(1));
} 
于 2012-09-12T09:44:54.203 回答
0

很抱歉,我所在的位置无法对此进行测试,但可以

\".*?(?:[^\\]\")\\s*=\\s*\".*?(?:[^\\]\")

工作?

我只是将其替换为 \"(?:[^\\]\") 因此如果它们之前的字符不再是 a,它们将不匹配\

于 2012-09-12T09:39:20.427 回答
-1
/"([^"\\]*(?:\\.[^"\\]*)*)"/

来源另请参阅this previous question

于 2012-09-12T09:58:26.847 回答