gpt4 book ai didi

java - 用于获取包含引号的字符串的正则表达式

转载 作者:行者123 更新时间:2023-12-01 08:48:00 24 4
gpt4 key购买 nike

我正在使用正则表达式来获取包含引号的字符串值。在下面的示例中,我想获取值摘要键为“Here is“summary”。目前我只得到“Here is ”作为下面程序的输出。我想转义第一个和最后一个双引号之间的所有双引号.

    String in = "summary = \"Here is \"summary\"";  

Pattern p = Pattern.compile("'(.*?)'|\"(.*?)[^\\\"]+\"");
Matcher m = p.matcher(in);

while(m.find()) {
System.out.println(m.group());
}

感谢您的帮助。

最佳答案

使用这个:

/\\["']((?:[^"\\]|\\.)*)\\["']/

演示:https://regex101.com/r/033EKx/1

记住:当尝试通过 " 构建正则表达式时,\ 应更改为 \\ 和其他特殊字符 ("')

rStr = "/\\\\[\"\']((?:[^\"\\\\]|\\\\.)*)\\\\[\"\']/" ;

关于java - 用于获取包含引号的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42558335/

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