Reputation: 157
I need to make groups of numbers and single alphabet from a given string, like:
15D12A3C11B12A
into
15D, 12A, 3C, 11B, 12A
and store the output individually in separate variables
There's only one alphabet between any two given numbers except in the end of the string and string always begins with a number. Alphabets can be A-Z, uppercase only.
Upvotes: 0
Views: 219
Reputation: 1367
The following code prints out the matches vertically. You just need to adjust it to get the desired result string. The regex means: 1..n digits followed by exactly one character from A-Z. If you want to support lower case characters, adjust it to \\d+[a-zA-Z]
String str = "15D12A3C11B12A";
String pattern = "\\d+[A-Z]";
Pattern p = Pattern.compile(pattern);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Upvotes: 0
Reputation: 129497
I would just use a Matcher
:
String str = "15D12A3C11B12A";
Matcher m = Pattern.compile("\\d+\\D").matcher(str);
while (m.find())
System.out.println(m.group());
15D 12A 3C 11B 12A
Upvotes: 4