我有一个带通配符X的模式字符串(例如:abc*)。
我还有一组字符串需要与给定的模式匹配。
例如:
abf - false abc_fgh - true abcgafa - true fgabcafa - false 我尝试使用正则表达式进行匹配,但它没有起作用。
这是我的代码:
我还有一组字符串需要与给定的模式匹配。
例如:
abf - false abc_fgh - true abcgafa - true fgabcafa - false 我尝试使用正则表达式进行匹配,但它没有起作用。
这是我的代码:
String pattern = "abc*";
String str = "abcdef";
Pattern regex = Pattern.compile(pattern);
return regex.matcher(str).matches();
这会返回false
还有其他方法可以使它工作吗?
谢谢
.*
代替*
,.
代替?
),然后使用String#matches()
函数即可。 - ThomasString::startsWith
方法。但最好的方法是使用正则表达式。 - SunflamestartsWith
。 - Scary Wombat