0

Possible Duplicate:
Using Regular Expressions to Extract a Value in Java

For example, the input string is AB100FF10. I need to read 100 and 10 from the string. Is there any classes/objects in Java that I can use?

5
  • Oh, I come to realize that I can use regular expression Commented Jul 5, 2011 at 17:41
  • I am asking the data structure that reads numeric data from combination of numeric and string Commented Jul 5, 2011 at 17:43
  • 1
    Take a look at stackoverflow.com/questions/237061/… Commented Jul 5, 2011 at 17:48
  • 2
    @SecureFish Data structures don't do these things, what you mean is objects/classes. Commented Jul 5, 2011 at 17:48
  • I come up with the solution without running it: ^.(0-9)+.$. Following is my explaination: any char among 0 to 9, at least one of them appear once or more than one time. before it or after it, any character matches Commented Jul 5, 2011 at 21:51

4 Answers 4

1

try this

String[] nums = "AB100FF10".split("\\D+");
for (String num : nums) {
    System.out.println(num);
}

Other than that, you could try passing the string to a class like Scanner

Scanner scan = new Scanner("AB100FF10").useDelimiter("\\D+");
while (scan.hasNextInt()) {
    System.out.println(scan.nextInt());
}

Edit: using \\D instead of \\w as a delimiter, as Bohemian suggested in his answer and comments.

Sign up to request clarification or add additional context in comments.

6 Comments

You beat me to it. I tried to delete my post. Not sure if it actually went though.
Have you tested your code? (Because it doesn't work)
:P Scanner is slow though. The post is deleted, but you should be able to see it, and restore it if you like.
@Bohemian no, I haven't, but this shouldn't be about spoonfeeding. The idea is there.
It's an idea, just not a good idea. Compare it with my answer (your regex isn't the best - for example your regex would try to parse a bracket ( as an integer.
|
0

Just use split(<non-digits>), like this:

String[] numbers = "AB100FF10CCC".replaceAll("(^\\D*|\\D*$)", "").split("\\D+"); // "[100, 10]"

Now numbers contains an array of Strings that are all guaranteed to be numeric. You can use Integer.parseInt() to get ints.

The replaceAll("(^\\D*|\\D*$)", "") is used to trim non-digits from the front and back of the input string, otherwise split() will give you a blank string as the first/last element. It just makes to code simpler, rather than having to test the first/last specially.

As a method, it would look like this:

public static int[] parseInts(String input) {        
    String[] numbers = input.replaceAll("(^\\D*|\\D*$)", "").split("\\D+");
    int[] result = new int[numbers.length];
    for (int i = 0; i < numbers.length; i++) {
        result[i] = Integer.parseInt(numbers[i]);
    }
    return result;
}

8 Comments

maybe from the front and back of input string it will be replaceAll("(^\\D*|\\w+\\D*$)", "")?
@Bohemian. Before running the code, I want to understand the mechanism correctly. what I will get after calling replaceAll(). Do I get "100FF10" or "100 10"? If I just get "100FF10", what will happen if I have "A100B1000C2000D3000"? In this case I need split all the integers embedded in a string?
It's all in my explanation: "[replaceAll] is used to trim non-digits from the front and back of the input string (etc)". ie "AB100FF10CCC" --> "00FF10". The code will work for any number of ints embedded.
@maks: no \D is the right choice: it means "non-digits"
@Bohemian: "AB100FF10CCC".replaceAll("(^\\D*|\\D*$)", "") will make 10010 string because interrnal FF also match the pattern
|
0

If you want to get only integers you can do the following:

ArrayList<Integer> numbers = new ArrayList<Integer>();
char[] characters = "AB100FF10".toCharArray();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < characters.length; i++) {
     if (Character.isDigit(characters[i])) 
        buf.append(characters[i]);
     else if (buf.length() != 0) {
         numbers.add(Integer.parseInt(buf.toString()));
         buf = new StringBuffer();
     }
}

After that you will have an arrayList of numbers

3 Comments

This is what I did in the interview. And the interviewer asked for more efficient way to do it.
Did interviewer ask you about this in the context of regular expressions?
Not exactly, I think he is expecting more concise code
0

I think pattern might be a better solution to because pattern object is more powerful comparing to simple String split() method.

for example, following code can resolve the same problem without any exception

Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(test[0]);
while(m.find()){
  int x = Integer.parseInt( m.group() );
}

But if I use String.split(), there is one NumberFormatException is hard to dealt with.For example, below code can't escape NumberFormatException

for(int i = 0 ; i < test.length; i++){
  String[] numstr= test[i].split("\\D+");
  try{              
    for(int j=0; j<numstr.length;j++){
      if( numstr[j] == null || numstr[j] == ""){
    System.out.println("empty string \n");
      }
      else
       Integer.parseInt(numstr[j]);                 
  }catch(NumberFormatException ie){
      ie.printStackTrace();
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.