Java 正则表达式与目标不匹配
假设我们有如下字符串列表-admin、sam1、sam2、max、maximusJava 正则表达式与目标不匹配,java,regex,Java,Regex,假设我们有如下字符串列表-admin、sam1、sam2、max、maximus Then in search box if I give *a*m* should return admin *sam* should return sam1,sam2 m* should return max,maximus max* should return max,maximus 等等 我的代码如下(示例代码,不查看列表):- 还有一些,输出为假,但必须为真:
Then in search box if I give
*a*m* should return admin
*sam* should return sam1,sam2
m* should return max,maximus
max* should return max,maximus
等等
我的代码如下(示例代码,不查看列表):-
还有一些,输出为假,但必须为真:
1. private static final String REGEX = ".*ad*.";
private static final String INPUT = "admin";
2. private static final String REGEX = ".*max*.";
private static final String INPUT = "max11";
3. private static final String REGEX = ".*sa*.";
private static final String INPUT = "sam1";
您的正则表达式是不正确的,正如在 这个正则表达式的意思是:
零个或多个字符*
- 文字
a
零个或更多d*
字符d
任何字符
.*ad.*
这意味着:
零个或多个字符*
- 文字
ad
零个或多个字符*
*ad*
更改为*ad*
?上面的注释指出了您的错误并解决了它。
.*ad*.
.*ad.*