I've got my own indexOf
function
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;
}
This function takes in a user provided pattern
and to see if the pattern
exists within the target
String. Sometimes a user could include regex characters like *** hello world
in the String and this causes the function to return a Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*'
error.
How can I treat the user generated String to overcome errors like these?