我想找到字符串中的以下模式:正则表达式模式不工作
"{xxxxx$ppp>5}&&{xxxxx.ppp==5}"
我用正则表达式:
"[{a-z$.a-z==><0-9}||&&{a-z$.a-z==><0-9}]"
但在这种情况下,字符"&&"
不包括在字符串。有人可以告诉正则表达式模式有什么问题
我想找到字符串中的以下模式:正则表达式模式不工作
"{xxxxx$ppp>5}&&{xxxxx.ppp==5}"
我用正则表达式:
"[{a-z$.a-z==><0-9}||&&{a-z$.a-z==><0-9}]"
但在这种情况下,字符"&&"
不包括在字符串。有人可以告诉正则表达式模式有什么问题
试试这个正则表达式:
// 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}
\\{[a-zA-Z]+\\$[a-zA-Z]+>5}&&{[0-9]+\\.[a-zA-Z]+==[0-9]+\\}
从看你以前的question,我假设你想要xxx和ppp是任何字母字符串,而5是任何数字字符串。
我在线程“main”java.util.regex.PatternSyntaxException中发生以下异常异常:非法重复 {[a-zA-Z] + \ $ [a-zA-Z] +> 5}(&&){{ [0-9] + \。[a-zA-Z] + == [0-9] +} \t at java.util.regex.Pattern.error(Unknown Source) \t at java.util.regex。 Pattern.closure(来源不明) \t在java.util.regex.Pattern.sequence(来源不明) \t在java.util.regex.Pattern.expr(来源不明) \t在java.util.regex.Pattern中。编译(未知源代码) – Phalguni
忘记逃离{},我的坏。 – Robadob
嘿它仍然会抛出异常。我自己找到了解决方案。正确的正则表达式是“[{a-zA-Z $ .a-zA-Z ==><0-9} \\ && {a-zA-Z $ .a-zA-Z ==><0-9 }]“ – Phalguni
请通过一些[正则表达式(HTTP:// WWW。 regular-expressions.info/tutorial.html)[教程](http://regexone.com/) –
你想找到确切的模式? – Robadob
{xxxxx \\ $ ppp> 5}(&&)?{xxxxx \\。ppp == 5} 匹配字符串“{xxxxx $ ppp> 5} && {xxxxx.ppp == 5}”和“{ xxxxx $ ppp> 5} {xxxxx.ppp == 5}“ – Robadob