LemonMan
LemonMan

Reputation: 3143

Get a group of numbers from a string

I have a file that contains strings, which in turn contain numbers of 10 digits. I need to extract the numbers, with regex and put them in an array. I think I can use \d{10} but I'm not sure how to actually apply that with Java.

Also, an additional element of complexity, is potentially if there are a lot of numbers there may be multiple numbers with different forms like 123456745-9 and 123456745-95 signifying a range. I'd like to extract those numbers as well. (I can handle creating the range of numbers in java, Regex is not necessary for that)

Any tips would be appreciated!

Upvotes: 0

Views: 815

Answers (2)

greedybuddha
greedybuddha

Reputation: 7507

The regex is simpler than you think. You just need to match any digit one or more times.

Example:

String line = "a line with some digits 123456745-9 and maybe some more 343-34 and a single 1 99 ";
String regexpattern = "(\\d+)(-(\\d+))?";
Pattern pattern = Pattern.compile(regexpattern);
Matcher matcher = pattern.matcher(line);
while (matcher.find()){
    System.out.println("number= '" + matcher.group(1)+"'");
    if (matcher.group(3) != null)
        System.out.println("range '" + matcher.group(3)+"'");
}

This output would be the following

number= '123456745'
ranges to '9'
number= '343'
ranges to '34'
number= '1'
number= '99'

Upvotes: 0

assylias
assylias

Reputation: 328598

You could split on non-digit characters but keep the -:

String[] numbers = input.split("[^\\-\\d]+");

Example:

String input = "bla bla bla 123456789 bla bla 123456789 bla bla 123456765-9 bla bla bla 123456767-89 bla bla";
input = input.replaceFirst("^[^\\-\\d]*", ""); //remove the leading non-digits if any
String[] numbers = input.split("[^\\-\\d]+"); //split
System.out.println(Arrays.toString(numbers));

outputs:

[123456789, 123456789, 123456765-9, 123456767-89]

Upvotes: 3

Related Questions