tags:

views:

102

answers:

4

Hello, I have got strings like:

BLAH00001

DIK-11

DIK-2

MAN5

so all the strings are a kind of (sequence any characters)+(sequence of numbers)

and i want something like this:

1

11

2

5

in order to get those integer values, i wanted to separate the char sequence and the number sequence an do something like Integer.parseInt(number_sequence)

Is there something that does this job?

greetings

A: 

Maybe you want to have a look to Java Pattern and Matcher:

Roberto Aloi
This is nigh on RTFM. It is better to provide more guidance.
Alain O'Dea
+1  A: 
 Pattern p = Pattern.compile("^[^0-9]*([0-9]+)$");
 Matcher m = p.matcher("ASDFSA123");
 if (m.matches()) {
    resultInt = Integer.parseInt(m.group(1)));
 }
Maurice Perry
That won't do it. Try it with the string in the original post, or something like `"foo 123 bar 456 baz"`. With a `Pattern/Matcher`, you only need to define the pattern you want to match and then keep calling `find()` on the `Matcher` inside a while-statement.
Bart Kiers
You may be right, it is not clear to me from the post if the goal is to parse one string at a time or the whole thing at once.
Maurice Perry
Ah yes, I see what you mean.
Bart Kiers
+4  A: 

Try this:

public class Main {
    public static void main(String[]args) {
        String source = "BLAH00001\n" +
                "\n" +
                "DIK-11\n" +
                "\n" +
                "DIK-2\n" +
                "\n" +
                "MAN5";
        Matcher m = Pattern.compile("\\d+").matcher(source);
        while(m.find()) {
            int i = Integer.parseInt(m.group());
            System.out.println(i);
        }
    }
}

which produces:

1
11
2
5
Bart Kiers
+1  A: 
String[] a ={"BLAH00001","DIK-11","DIK-2","MAN5"};
 for(String g:a)
  System.out.println(Integer.valueOf(g.split("^[A-Z]+\\-?")[1]));

 /*******************************  
   Regex Explanation :
     ^  --> StartWith
    [A-Z]+ --> 1 or more UpperCase
    \\-? --> 0 or 1 hyphen   
*********************************/
Emil