views:

122

answers:

2

Hi,

I have an input string with a very simple pattern - capital letter, integer, capital letter, integer, ... and I would like to separate each capital letter and each integer. I have been dealing with it for quite a while and can't figure out what is the best way to do that in Java, I have already tried regexp using Pattern and Matcher, then StringTokenizer, but still no success.

This is what I want to do, showed in Python:

for token in re.finditer( "([A-Z])(\d*)", inputString):
      print token.group(1)
      print token.group(2)

For input "A12R5F28" the result would be:

A

12

R

5

F

28

Thank you very much. Tomas

+4  A: 

You could use regex API in Java and achieve the same functionality:

Pattern myPattern = Pattern.compile("([A-Z])(\d+)")
Matcher myMatcher = myPattern.matcher("A12R5F28");
while (myMatcher.find()) {
      // Do your stuff here
}
Ravi Gummadi
@Ravi, your RE has Unbalanced Parentheses.)
st0le
Thank you, it works perfectly, my problem was that I was using myMatcher.matches() instead of myMatcher.find()
Tomas Novotny
@St0le - Thanks, corrected it!
Ravi Gummadi
Since the integer part is required, I would change `\d*` (0 or more digits) to `\d+` (1 or more digits).
Paul McGuire
@Paul Thank you, modified it.
Ravi Gummadi
+2  A: 

Expanding on Ravi's Answer....

Pattern myPattern = Pattern.compile("([A-Z])(\\d+)");
Matcher myMatcher = myPattern.matcher("A12R5F28");
while (myMatcher.find()) {
  System.out.println(myMatcher.group(1) + "\n" + myMatcher.group(2));
}
st0le