gpt4 book ai didi

java - 如何在java中匹配紧邻的前一个单词模式?

转载 作者:行者123 更新时间:2023-12-02 05:59:53 25 4
gpt4 key购买 nike

我有一个像这样的字符串:

"value of <abc/def> ends with <def> and value of <abc/def> does not end with <abc> and value of <abc> is <abc>"

我想提取字符串value of <abc/def> does not end with <abc>
我使用了以下模式

String text = "value of <abc/def> ends with <def> and value of <abc/def> does not end with <abc> and value of <abc> is <abc>";
String patternString1 = "(value\\s+of\\s+(<.*>)\\s+ends\\s+with\\s+(<.*>))";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
System.out.println("found: " + matcher.group(1));
}

结果:-

found: value of <abc/def> ends with <def> and value of <abc/def> does not end with <abc> and value of <abc> is <abc>

最佳答案

正如你的意思是得到 value of <abc/def> does not end with <abc>修正后的图案:

String patternString1 = "(value\\s+of\\s+(<[^>]*>)\\s+does\\s+not\\s+end\\s+with\\s+(<[^>]*>))";

关于java - 如何在java中匹配紧邻的前一个单词模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759983/

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