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

前端 未结 7 1602
臣服心动
臣服心动 2021-01-17 16:02

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 arra

相关标签:
7条回答
  • 2021-01-17 16:08

    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).
    0 讨论(0)
  • 2021-01-17 16:13

    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());
    }
    
    0 讨论(0)
  • 2021-01-17 16:14

    You may try this:

    reg.split("asd0085 sa223 9349x").replace("^/", "")
    
    0 讨论(0)
  • 2021-01-17 16:17

    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).

    0 讨论(0)
  • 2021-01-17 16:25
    Pattern reg = Pattern.compile("\\D+");
    // ...
    

    results in:

    0085
    223
    9349
    
    0 讨论(0)
  • 2021-01-17 16:27

    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.

    0 讨论(0)
提交回复
热议问题