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?
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
[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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.