14

I want to find all numbers from a given string (all numbers are mixed with letters but are separated by space).I try to split the input String but when check the result array I find that there are a lot of empty Strings, so how to change my split regex to remove this empty spaces?

Pattern reg = Pattern.compile("\\D0*");
String[] numbers = reg.split("asd0085 sa223 9349x");
for(String s:numbers){
    System.out.println(s);
}

And the result:

85


223
9349

I know that I can iterate over the array and to remove empty results. But how to do it only with regex?

2
  • Do you want to just get the numeric string from there? Commented Aug 22, 2014 at 16:20
  • With Guava, you can do Splitter.onPattern(pattern).omitEmptyStrings().split(string); Commented Aug 24, 2016 at 21:14

7 Answers 7

15

If you are using java 8, you can do it in 1 statement like this:

String[] array = Arrays.asList(s1.split("[,]")).stream().filter(str -> !str.isEmpty()).collect(Collectors.toList()).toArray(new String[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

How about Stream.of(s.split(" ")).filter(w -> !w.isEmpty()).toArray(String[]::new);
8

Don't use split. Use find method which will return all matching substrings. You can do it like

Pattern reg = Pattern.compile("\\d+");
Matcher m = reg.matcher("asd0085 sa223 9349x");
while (m.find())
    System.out.println(m.group());

which will print

0085
223
9349

Based on your regex it seems that your goal is also to remove leading zeroes like in case of 0085. If that is true, you can use regex like 0*(\\d+) and take part matched by group 1 (the one in parenthesis) and let leading zeroes be matched outside of that group.

Pattern reg = Pattern.compile("0*(\\d+)");
Matcher m = reg.matcher("asd0085 sa223 9349x");
while (m.find())
    System.out.println(m.group(1));

Output:

85
223
9349

But if you really want to use split then change "\\D0*" to \\D+0* so you could split on one-or-more non-digits \\D+, not just one non-digit \\D, but with this solution you may need to ignore first empty element in result array (depending if string will start with element which should be split on, or not).

Comments

2

You can try with Pattern and Matcher as well.

Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("asd0085 sa223 9349x");
while (m.find()) {
    System.out.println(m.group());
}

Comments

2

The method i think to solve this problem is,

String urStr = "asd0085   sa223 9349x";
urStr = urStr.replaceAll("[a-zA-Z]", "");
String[] urStrAry = urStr.split("\\s");
  1. Replace all alphabets from the string.
  2. Then split it by whitespace (\\s).

Comments

1
Pattern reg = Pattern.compile("\\D+");
// ...

results in:

0085
223
9349

Comments

1

You may try this:

reg.split("asd0085 sa223 9349x").replace("^/", "")

Comments

1

Using String.split(), you get an empty string as array element, when you have back to back delimiter in your string, on which you're splitting.

For e.g, if you split xyyz on y, the 2nd element will be an empty string. To avoid that, you can just add a quantifier to delimiter - y+, so that split happens on 1 or more iteration.

In your case it happens because you've used \\D0* which will match each non-digit character, and split on that. Thus you've back to back delimiter. You can of course use surrounding quantifier here:

Pattern reg = Pattern.compile("(\\D0*)+");   

But what you really need is: \\D+0* there.

However, if what you only want is the numeric sequence from your string, I would use Matcher#find() method instead, with \\d+ as regex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.