Am I misinterpreting something regarding Java regexes? Shouldn’t the following match the leading zero:
public class Testit {
public static void main(String[] args) {
format("0115724848");
}
private static void format(String elementToFormat) {
if (elementToFormat.matches("^0")) {
System.out.println("leading zero:" + elementToFormat);
} else {
System.out.println("no leading zero:" + elementToFormat);
}
}
}
matchestries to match the pattern against the whole of the input string… and your input string isn’t just “beginning of string followed by 0”.Either you need
"0.*"(the^is unnecessary precisely becausematcheswill match the whole string) or you could create aPatternand then use:Of course it’s not clear why you’re using a regex here at all, in that you can use: