Reputation: 2515
I am using the following pattern to match my String:
[a-zA-Z0-9]*
Even when I am passing the String *$#
, its getting matched by the regular expression. Could someone explain what am I doing wrong or why this is happening?
Upvotes: 0
Views: 1788
Reputation: 128919
[a-zA-Z0-9]*
means 0 or more of any of those characters. If you're using Matcher.find(), it'll find that anywhere/everywhere because it can match anywhere in a string.
Upvotes: 2
Reputation: 32817
You should use ^
(start of the string) and $
(end of the string).
So,the regex would be
^[a-zA-Z0-9]*$
[a-zA-Z0-9]*
would match anywhere in the string if you use find
method..Using ^
and $
would match the entire input from start till end
if you use matches
method you don't need to have ^
,$
as it tries to match the entire string
Upvotes: 3