[++]
matches a +
or a +
*+
matches the [++]
zero or more times, possessively
Question
I'm using libphonenumber and I try to check some phone numbers if they are valid with regexp pattern VALID_PHONE_NUMBER which can be found here and it looks like this
private static final String VALID_PHONE_NUMBER =
DIGITS + "{" + MIN_LENGTH_FOR_NSN + "}" + "|" +
"[" + PLUS_CHARS + "]*+(?:[" + VALID_PUNCTUATION + STAR_SIGN + HASH_SIGN +"]*" + DIGITS + "){3,}[" +
VALID_PUNCTUATION + STAR_SIGN + HASH_SIGN + VALID_ALPHA + DIGITS + "]*";
On my Android phone this expression is compiled to following
\p{Nd}{1}|[++]*+(?:[-x--?-?--/ ?? ()()[].\[\]/~?~~*#]*\p{Nd}){3,}[-x--?-?--/ ?? ()()[].\[\]/~?~~*#DEFGABCLMNOHIJKUTWVQPSRYXZdefgabclmnohijkutwvqpsryxz\p{Nd}]*
What does it mean [++]*+
Does it mean "plus-or-plus zero-or-more-times and then plus?
Does it make any sense to have two pluses next to each other?
Solution
[++]
matches a +
or a +
*+
matches the [++]
zero or more times, possessively
OTHER TIPS
The rightmost +
is fairly new. As others have said, [++]
means "find a single +
". with the *
quantifier: [++]*
means "zero or more plus signs".
The extra rightmost +
in [++]*+
is not supported by all Regular Expression engines, but means "do not do any backtracking", i.e. do a "maximum match". Contrast with ?
, which does a minimum match.
See also this question
+
within character class is treated as a literal..It should be [+]*+
*+
is Possessive Quantifier..It doesn't backtrack..
A character class ([...]
) ignores duplicate characters.
Therefore, [++]
is equivalent to \+
.
The second +
is redundant both inside and outside the character class.
Within the character class [++]
it's just ignored. Outside the character class it acts as a quantifier (one or more) but isn't valid because except when preceded with another quantifier like *
here (for zero or more) as then it denotes possessiveness.
More info at Possessive Quantifiers
You can make a quantifier possessive by placing an extra + after it. E.g. * is greedy, *? is lazy, and *+ is possessive. ++, ?+ and {n,m}+ are all possessive as well.