Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So "XXXXX**".matches("[X{9,11}\*{2,3}]") returns false as expected... But, "XXXXX**".matches("[X{9,11}\*{2,3}]+") returns true. Am I using the + quantifier correctly? (I want the second one to also return false)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

[...] matches any character defined in the character class, so [X{9,11}\*{2,3}] actually means, a single character which is: X, or open brace, or 9, or comma, or 1, or 1 (yes you have it duplicated), or backslash, or asterisk....

So as your string have more than character in your string to-be-matched, such pattern will not match.

When you add a +, it means matching a string with 1 or more [ X or asterisk or....], so it match

I believe what you really want to do is using a group.

So the regex looks like (X{9,11}*{2,3}])+


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...