What is the 开发者_运维问答java regex to match the below pattern?
<anyString>.<5 or 10>.anyNumber.anyNumber
Here 5 and 10 are numbers.
.*\.(?:5|10)\.\d+\.\d+
should work.
Explanation:
.* # any number of characters (except newlines)
\. # literal dot
(?:5|10) # 5 or 10
\. # literal dot
\d+ # one or more digits
\. # literal dot
\d+ # one or more digits
Remember that, if you use it in Java, you need to escape the backslashes when constructing the regex:
Pattern regex = Pattern.compile(".*\\.(?:5|10)\\.\\d+\\.\\d+");
Assuming that i read your intention correctly this should work:
Pattern p = Pattern.compile(".*?\\.(?:5|10)\\.(\\d+)\\.(\\d+)");
Matcher m;
m = p.matcher(".5.11.10");
m.matches(); // == true
m.group(1).equals("11");
m.group(2).equals("10");
m = p.matcher("hannib al.10.11.12");
m.matches(); // == true
m.group(1).equals("11");
m.group(2).equals("12");
Assuming you mean the example valid string .5.1.1
This form should do the trick
\.(5|10)\.[0-9]\.[0-9]
or if the second two numbers can have more than one character i.e. .10.123.1234
\.(5|10)\.[0-9]*\.[0-9]*
精彩评论