How to remove empty results after splitting using regex in Java?

I want to find all numbers from a given string (all numbers are mixed with letters, but separated by a space). I am trying to split the input string, but when I check the result array, I find that there are many empty lines, so how do I change my split regex to remove this empty space?

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 an array and delete empty results. But how to do it only with regular expression?

+5
source share
7 answers

Do not use split . Use the find method, which returns 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 regular expression, it seems that your goal is also to remove leading zeros, for example in the case of 0085 . If so, you can use a regular expression, for example 0*(\\d+) , and take part corresponding to group 1 (in brackets), and also match leading zeros outside this group.

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

Conclusion:

 85 223 9349 

But if you really want to use split , then change "\\D0*" to \\D+0* so that you can split into one or more non-digital digits \\D+ , and not just one digit \\D , but with with this solution, you may need to ignore the first empty element in the result array (depending on whether the line starts with the element to be split or not).

+5
source

If you are using java 8, you can do this in the 1st expression as follows:

 String[] array = Arrays.asList(s1.split("[,]")).stream().filter(str -> !str.isEmpty()).collect(Collectors.toList()).toArray(new String[0]); 
+6
source

You can also try with Pattern and Matcher .

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

The method that I decided to solve this problem,

 String urStr = "asd0085 sa223 9349x"; urStr = urStr.replaceAll("[a-zA-Z]", ""); String[] urStrAry = urStr.split("\\s"); 
  • Replace all alphabets from the string.
  • Then separate it with a space ( \\s ).
+2
source
 Pattern reg = Pattern.compile("\\D+"); // ... 

leads to:

 0085 223 9349 
+1
source

You can try the following:

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

Using String.split() , you will get an empty string as an element of the array when you have back to back in your string on which you are splitting.

For example, if you divide xyyz by y , the second element will be an empty string. To avoid this, you can simply add a quantifier to the delimiter - y+ , so that the decay occurs at 1 or more iterations.

In your case, this is due to the fact that you used \\D0* , which will correspond to each asymmetric character and is divided into it. So you have back to back. You can, of course, use the surrounding quantifier here:

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

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

However, if you only need the number sequence from your string, I would use the Matcher#find() method, and \\d+ as a regular expression.

+1
source

Source: https://habr.com/ru/post/1200943/


All Articles