我有自己的indexOf功能
private static int indexOf(String target, String pattern){
Pattern p = Pattern.compile(pattern);
Matcher matcher = p.matcher(target);
if(matcher.find()){
return matcher.start();
}
return -1;
}
此函数接受用户提供的pattern并查看字符串中是否pattern存在。target有时用户可能会在字符串中包含正则表达式字符*** hello world,这会导致函数返回Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*'错误。
如何处理用户生成的字符串以克服此类错误?