saint
saint

Reputation: 268

Count occurrence of leading zeros before a number using pattern

A very simple question. I need to get the number of trailing zeros before the occurence of first number i.e:

0000450 => 4
00632000670 => 2
77830 => 0

Here is what I have tried already, thanks to already answerd question:

public static Integer trailingZeros(String s){
    Pattern patt = Pattern.compile("(0+)$");
    Matcher matcher = patt.matcher(String.valueOf(s));
    Integer trailingZeroes = 0;
    if (matcher.find()) {
        trailingZeroes = matcher.group(1).length();
    }
    return trailingZeroes;
}

Upvotes: 2

Views: 1143

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Issue : (0+)$ here it mean capture atlease one or more zeros, then end of string mean it will find match only of 0 or 00000 more zeros , everything else will not be matched

  • (^0*)\\d* capture ^ starting with

    • 0* : zero or more occurrence of 0
  • \d* : zero or more digits

public static Integer trailingZeros(String s){
        Pattern patt = Pattern.compile("(^0*)\\d*");
        //                              ^   ^ group 1
        Matcher matcher = patt.matcher(String.valueOf("77830"));
        matcher.find()
    return matcher.group(1).length();
    //     simply return the length 
    // will return zero in case of no match found 
}

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

If you need to count initial zeros, you may even use a non-regex solution like

public static int countInitialZeros(String haystack) {
    for (int i=0; i < haystack.length(); i++)
    {
        if (haystack.charAt(i) != '0') { // if current char is not 0
            return i;                    // return the index, it the count
        }
    }
    return haystack.length();          // If we get here, the whole string is made of zeros
}

See the online Java demo.

Test:

System.out.println(countInitialZeros("0005606")); // => 3
System.out.println(countInitialZeros("005606"));  // => 2
System.out.println(countInitialZeros("05606"));   // => 1
System.out.println(countInitialZeros("5606"));    // => 0
System.out.println(countInitialZeros("0000"));    // => 4

NOTE: if you really need to make sure the string only consists of digits, you may use s.matches("\\d+") before counting the zeros.

Here is a regex approach that both makes sure the string is made of digits and returns the count of initial zeros:

public static int countInitialZeros(String haystack) {
    return haystack.replaceFirst("^(0*)[1-9]\\d*$", "$1").length();
}

See another demo. The ^(0*)[1-9]\\d*$ matches

  • ^ - start of string
  • (0*) - Group 1: zero or more 0s
  • [1-9] - a digit from 1 to 9
  • \\d* - zero or more digits
  • $ - the end of string.

And if you want to return a -1 for a string that does not only consist of digits you might use

public static int countInitialZeros(String haystack) {
    return haystack.matches("\\d+") ? haystack.replaceFirst("^(0*)[1-9]\\d*$", "$1").length() : -1;
}

See this Java demo. countInitialZeros("abc") returns -1.

Upvotes: 2

Related Questions