Solution:
(also take a look at the edit that I made at the end of my answer)
"\\b(\\d{1,3})([a-z]{1,3})(?=,*|\\b)"
Example:
String s = "1a, 12a, 1ab, 12ab, 123a, 123abc";
Pattern p = Pattern.compile("\\b(\\d{1,3})([a-z]{1,3})(?=,*|\\b)");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println("Group: "+ m.group() + ", letters: " + m.group(1) + ", digits: " + m.group(2));
}
Output that you get:
Group: 1a, letters: 1, digits: a
Group: 12a, letters: 12, digits: a
Group: 1ab, letters: 1, digits: ab
Group: 12ab, letters: 12, digits: ab
Group: 123a, letters: 123, digits: a
Group: 123abc, letters: 123, digits: abc
Explanation:
\\b(\\d{1,3})([a-z]{1,3})(?=,*|\\b)
whole regex
\\b
- word boundary
\\d{1,3}
- digit, from one to three times
[a-z]{1,3}
- characters from a
to z
from one to three times
(?=,*|\\b)
- this is positive lookahead, you say that after these letters you want to be present ,
or word boundary, but you don't want them to be present in the matching group (called with m.group()
)
()
- matching groups are in parenthesis - in my regex I used two matching groups: #1: (\\d{1,3})
#2: ([a-z]{1,3})
(they are printed with m.group(1)
and m.group(2)
)
If you're not very familiar to regular expressions syntax yet, you might want to have a look at Java API Documentation of class Pattern. There is a list of available uses of regular expressions. It's worth giving regular expressions a try, as it might save a lot of your time when working with Strings in the future.
Edit:
Actually this regex can be changed to:
(?<=\\b)(\\d{1,3})([a-z]{1,3})(?=\\b)
There is a positive lookbehind (?<=\\b)
- it means that you want digits to preceded by word boundary (including commas in the lookahead and lookbehind was redundant so I deleted it).