Reputation: 120
I have some strings:
And I want to separate 19 with ("x" or "." one or more times) and 100. I tried to use regex:
(x|\.)+
to group but it can not match all case above. Which regex can match all case above?
Upvotes: 2
Views: 5027
Reputation: 626903
You may use
(\d+)(\s*[x.][x.\s]*)(\d+)
See the regex demo
Details
(\d+)
- Group 1: one or more digits(\s*[x.][x.\s]*)
- Group 2:
\s*
- 0+ whitespaces[x.]
- an x
or .
[x.\s]*
- 0+ chars that are either x
, .
or whitespace chars(\d+)
- Group 3: one or more digitsUpvotes: 2
Reputation: 163362
For your example, you could capture what is before 100 and 100 in 2 capturing groups:
Explanation
(
\d+
x
, .
or whitespace [x. ]+
using a character class)
(\d+)
Upvotes: 1
Reputation: 1308
You can use regex:
public static Matcher split(String text) {
Pattern pattern = Pattern.compile("(\\d+)(x*)(\\s*)(\\.*)(\\d+)");
Matcher matcher = pattern.matcher(text);
matcher.find();
return matcher;
}
This regex works for test data:
19xx.100
19x.100
19..100
19x .100
After then matcher.group(1)
will return 19
and matcher.group(5)
will return 100
Upvotes: 1