lundi 7 novembre 2016

Java pattern usage

I am trying to get whitespaces at the beginning of a line. I want to get exact number of white spaces matcher true. My string is " WhiteSpace". Problem is both matcher and matcher2 pattern matches work on this string.

The thing i want is;
A pattern that only get 1 white space, but this pattern should not work for 2 white space string. In the scenario below both matcher and matcher2 are true. But matcher should be false, matcher2 should be true.

I want matcher to be true for :" WhiteSpace",
I want matcher 2 to be true for :" WhiteSpace",
I want matcher to be false for :" WhiteSpace",


SCENARIO :

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher("  WhiteSpace");

Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher("  WhiteSpace");

Thanks,

Aucun commentaire:

Enregistrer un commentaire