如何匹配java中的前一个单词模式?

如何匹配java中的前一个单词模式?,java,regex,string,Java,Regex,String,我有这样一个字符串: "value of <abc/def> ends with <def> and value of <abc/def> does not end with <abc> and value of <abc> is <abc>" “值以结尾,值不以结尾,值为” 我想提取字符串的值不以结尾 我使用了下面的模式 String text = "value of <abc/def> ends with

我有这样一个字符串:

"value of <abc/def> ends with <def> and value of <abc/def> does not end with <abc> and value of <abc> is <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));
}
String text=“值以结尾,值不以结尾,值为”;
字符串模式String1=“(值\\s+of\\s+()\\s+结束\\s+和\\s+())”;
Pattern=Pattern.compile(patternString1);
Matcher Matcher=pattern.Matcher(文本);
while(matcher.find()){
System.out.println(“发现:+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>
找到:的值以结尾,的值不以结尾,的值为

尝试使用下面的代码

String text = "value of ends with and value of does not end with and value of is ";
String arr[] = text.split(" and ");
System.out.println(arr[1]);

这将字符串分成三部分。这将对您有所帮助。

因为您的意思是获得的
值不会以校正后的模式结束:

String patternString1 = "(value\\s+of\\s+(<[^>]*>)\\s+does\\s+not\\s+end\\s+with\\s+(<[^>]*>))";
String patternString1=“(value\\s+of\\s+(]*>)\\s+does\\s+not\\s+end\\s+with\\s+(]*>)”;
使用
.endsWith()

if(!.endsWith(){
//做些事情。。。
}

我把
放进去,这样它只会在不以结尾时才会发生。默认情况下,
*
是。你需要通过添加
来让它变懒。至少我认为这是问题所在-我不太理解这个问题。我真的不知道你真正的意思,但也许修改
*
的贪婪会起作用rick,to
*?
。这将获取尽可能少的字符。为什么您的正则表达式试图匹配
-部分结尾,而您说,您想匹配
而不是以
-部分结尾?这有点让人困惑。谢谢……但问题是所有字符之间都是变量。任何字符串都可以出现。喜欢或。因此我们不要使用静态字符串进行拆分。
if (!<abc/def>.endsWith(<abc>) {
   // do stuff ...
}