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?

Community
  • 1
  • 1
SecureFish
  • 2,391
  • 7
  • 32
  • 43
  • Oh, I come to realize that I can use regular expression – SecureFish Jul 05 '11 at 17:41
  • I am asking the data structure that reads numeric data from combination of numeric and string – SecureFish Jul 05 '11 at 17:43
  • 1
    Take a look at http://stackoverflow.com/questions/237061/using-regular-expressions-to-extract-a-value-in-java – Marcelo Jul 05 '11 at 17:48
  • 2
    @SecureFish Data structures don't do these things, what you mean is objects/classes. – Abdullah Jibaly Jul 05 '11 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 – SecureFish Jul 05 '11 at 21:51

4 Answers4

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.

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
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;
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • maybe from the front and back of input string it will be replaceAll("(^\\D*|\\w+\\D*$)", "")? – maks Jul 05 '11 at 22:27
  • @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? – SecureFish Jul 05 '11 at 23:20
  • 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. – Bohemian Jul 06 '11 at 03:06
  • @maks: no `\D` is the right choice: it means "non-digits" – Bohemian Jul 06 '11 at 03:09
  • @Bohemian: "AB100FF10CCC".replaceAll("(^\\D*|\\D*$)", "") will make 10010 string because interrnal FF also match the pattern – maks Jul 06 '11 at 15:17
  • @maks no it won't: the regex only matches on the **END** of the string - see the little `^` and `$`? – Bohemian Jul 06 '11 at 19:37
  • @Bohemian: Does the ^\D* match at the beginning of the string and the \D*$ at the end? – maks Jul 06 '11 at 22:13
  • @maks yes `^` means start of input, `$` means end of input, \D* means 0-n non-digits. Put them together using regex `OR` (ie `(A|B)` means `A OR B`) and you trim all non-digits from the start and end of the input – Bohemian Jul 07 '11 at 04:18
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

maks
  • 5,911
  • 17
  • 79
  • 123
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();
  }
}
maks
  • 5,911
  • 17
  • 79
  • 123
SecureFish
  • 2,391
  • 7
  • 32
  • 43