user3566896
user3566896

Reputation: 97

Capturing numbers with regex

I have these strings that contain sets of digits. What I need to do is capture every set of digits and create new strings for them. For example, in the string: "60 32 28 Some Characters 0 0 0" I need to capture and place 60, 32, 28, 0, 0, 0 into separate strings. Here is some of the code I have already tried:

public class First {

public static void main(String[] args) {

    String one = "60 32 28 Some Characters 0 0 0";


    Pattern a = Pattern.compile("[0-9]{2}.*?([0-9]{2}).*?([0-9]{2})");      
    Matcher b = a.matcher(one);
    b.find();

    String work = b.group();
    String work1 = b.group(1);
    String work2 = b.group(2);

    System.out.println("this is work: " + work);
    System.out.println("this is work1: " + work1);
    System.out.println("this is work2: " + work2);

    Pattern c = Pattern.compile("([0-9]{2})|([0-9])");      
    Matcher d = c.matcher(one);
    d.find();

    String work3 = d.group();
    System.out.println(work3);



}

}

However, I am unable to capture every digit. I have looked around other tutorials, but I am unable to find what I am doing wrong with my regex, or if there is another solution besides using regex. I have stayed away from using substrings because the text between digits usually vary in length. Any help would be appreciated.

Upvotes: 0

Views: 86

Answers (5)

Richard Neish
Richard Neish

Reputation: 8806

Simply loop over the matches() method of your Matcher. This code prints each matching number:

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String input = "60 32 28 Some Characters 0 0 0";

        Pattern a = Pattern.compile("\\D*(\\d+)");      
        Matcher b = a.matcher(input);
        List<String> nums = new ArrayList<String>();
        while (b.find()) {
               System.out.println("Matched " + b.group(1));
                nums.add(b.group(1));
        }
    }
}

Upvotes: 0

ajb
ajb

Reputation: 31689

String[] strings = one.split("[^\\d]+");

This treats every sequence of one or more non-digits as a delimiter, and returns an array of the results. Pretty much exactly what you want, right?

This also works, but I usually forget about the built-in character classes that means "not" (thanks, @Pshemo):

String[] strings = one.split("\\D+");

One caveat: the first element of Strings might be an empty string. This happens if the first character is not a digit. From @Ruslan Ostafiychuk, here's how we can fix it by stripping off the leading nondigits:

String[] strings = one.replaceFirst("^\\D+","").split("\\D+");

Upvotes: 8

AlexR
AlexR

Reputation: 115328

As far as I understand you have string that contains space delimited numbers. If this is correct may I suggest you to split the string by whitespace:

String[] strNums = str.split("\\s+");

Now if your original string was 60 32 28 Some Characters 0 0 0 your array will contain: 60, 32, 28, Some, Characters, 0, 0, 0.

Now iterate over this array and take only matching elements:

List<Integer> numbers = new ArrayList<>();
for (String s : strNums) {
   try {
        numbers.add(Integer.parseInt(s));
   } catch (NumberFormatException e) {
        // ignore
   }
}

Upvotes: 0

vandale
vandale

Reputation: 3650

tyr the following:

Pattern a = Pattern.compile("([0-9]{1,2})\\D*([0-9]{1,2})\\D*([0-9]{1,2})");
Matcher b = a.matcher(one);
while (b.find()) {

    String work = b.group(1);
    String work1 = b.group(2);
    String work2 = b.group(3);

    System.out.println("this is work: " + work);
    System.out.println("this is work1: " + work1);
    System.out.println("this is work2: " + work2);

}

output:

this is work: 60

this is work1: 32

this is work2: 28

this is work: 0

this is work1: 0

this is work2: 0

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

try this:

        Pattern c = Pattern.compile("([0-9][0-9]) | [0-9]");      
        Matcher d = c.matcher(one);
        while(d.find()) {
               System.out.println(d.group());
        }

It will match 2 digits and 1 digit numbers.

result:

60 
32 
28 
 0
 0
 0

Upvotes: 3

Related Questions