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

时间:2014-03-31 11:05:30

标签: 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>"

我想提取字符串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>

3 个答案:

答案 0 :(得分:2)

你的意思是在校正之后获得value of <abc/def> does not end with <abc>模式:

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

答案 1 :(得分:0)

请尝试使用以下代码。

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

这将字符串拆分为三个部分。这对你有帮助。

答案 2 :(得分:0)

使用.endsWith()

if (!<abc/def>.endsWith(<abc>) {
   // do stuff ...
}

我放了!所以只有当它没有以

结尾时才会发生