正则表达式模式不起作用

时间:2013-07-28 15:07:26

标签: java regex string

我想在字符串中找到以下模式:

"{xxxxx$ppp>5}&&{xxxxx.ppp==5}"

我使用了正则表达式:

"[{a-z$.a-z==><0-9}||&&{a-z$.a-z==><0-9}]"

但在这种情况下,字符"&&"不包含在字符串中。有人告诉正则表达式模式有什么问题

2 个答案:

答案 0 :(得分:0)

\\{[a-zA-Z]+\\$[a-zA-Z]+>5}&&{[0-9]+\\.[a-zA-Z]+==[0-9]+\\}

通过查看您之前的question,我假设您希望xxx和ppp为任何字母字符串,并且5为任何数字字符串。

答案 1 :(得分:0)

试试这个正则表达式:

// prints true
System.out.println("{xxxxx$ppp>5}&&{xxxxx.ppp==5}"
      .matches("\\{[a-z]+\\$[a-z]+>[0-9]\\}&&\\{[a-z]+\\.[a-z]+==[0-9]\\}"));

编辑 :(回应OP下面的评论)

String input = "{xxxxx$ppp>5}&&{xxxxx.ppp==5}";
Matcher matcher = Pattern.compile("\\{[a-z0-9]+[$.][a-z0-9]+[><=]=?[0-9]\\}")
                         .matcher(input);
while (matcher.find()) {
    System.out.println(matcher.group()); // {xxxxx$ppp>5}
}                                        // {xxxxx.ppp==5}