[英]Regular Expression allowing special characters
I am using the following pattern to match my String: 我正在使用以下模式来匹配我的字符串:
[a-zA-Z0-9]* [a-zA-Z0-9] *
Even when I am passing the String *$#
, its getting matched by the regular expression. 即使当我传递String *$#
,它也会被正则表达式匹配。 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 如果使用find
方法, [a-zA-Z0-9]*
将匹配字符串中的任何位置 。使用^
和$
将匹配从头到尾的整个输入
if you use matches
method you don't need to have ^
, $
as it tries to match the entire string 如果使用matches
方法,则不需要^
, $
因为它会尝试匹配整个字符串
[a-zA-Z0-9]*
means 0 or more of any of those characters. [a-zA-Z0-9]*
表示这些字符中的0个或多个。 If you're using Matcher.find() , it'll find that anywhere/everywhere because it can match anywhere in a string. 如果您使用Matcher.find() ,它将在任何地方/任何地方找到它,因为它可以匹配字符串中的任何地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.