Reputation: 10961
I want to split a string with the following patterns:
abc3xab -> ('abc', '3xa', 'b')
abc3x3xab -> ('abc', '3x3x', 'ab')
abc3xxab -> ('abc', '3xx', 'ab')
abc34xxab -> ('abc', '34xx', 'ab')
abc100ab -> ('abc100ab')
abc10axb -> ('abc10axb')
That's it, the pattern is '[0-9]+x[a-z]' (if there's a number followed by an 'x'
and any char between a-z
.
I tried
String[] words = string.split("[0-9]+x")
but then I only got abc
and ab
. What would be the correct way to do it?
Upvotes: 2
Views: 70
Reputation: 72854
Try using a combination of positive lookahead and lookbehind to keep the delimiter:
String[] words = string.split("((?<=[0-9]+x)|(?=[0-9]+x))");
String[] words = "abc3xab".split("((?<=[0-9]+x)|(?=[0-9]+x))");
System.out.println(Arrays.toString(words));
String[] words1 = "abc3xxab".split("((?<=[0-9]+x)|(?=[0-9]+x))");
System.out.println(Arrays.toString(words1));
String[] words2 = "abc34xxab".split("((?<=[0-9]+x)|(?=[0-9]+x))");
System.out.println(Arrays.toString(words2));
String[] words3 = "abc100ab".split("((?<=[0-9]+x)|(?=[0-9]+x))");
System.out.println(Arrays.toString(words3));
String[] words4 = "abc10axb".split("((?<=[0-9]+x)|(?=[0-9]+x))");
System.out.println(Arrays.toString(words4));
Results:
[abc, 3x, ab]
[abc, 3x, xab]
[abc, 3, 4x, xab]
[abc100ab]
[abc10axb]
Upvotes: 2