gpt4 book ai didi

java - 提取引号之间的子字符串,忽略\"

转载 作者:行者123 更新时间:2023-11-30 04:40:42 31 4
gpt4 key购买 nike

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

"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);

目前,我可以获得“=”的左右部分。但是当我的子字符串包含“\”“时,我的正则表达式无法正常工作。

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

提前致谢。

最佳答案

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));
}

关于java - 提取引号之间的子字符串,忽略\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384897/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com