Basé sur la réponse de @Andreas Dolk, enveloppé dans du code prêt à copier-coller :
/**
* Index of using regex
*/
public static int indexOfByRegex(CharSequence regex, CharSequence text) {
return indexOfByRegex(Pattern.compile(regex.toString()), text);
}
/**
* Index of using regex
*/
public static int indexOfByRegex(Pattern pattern, CharSequence text) {
Matcher m = indexOfByRegexToMatcher(pattern, text);
if ( m != null ) {
return m.start();
}
return -1;
}
/**
* Index of using regex
*/
public static Matcher indexOfByRegexToMatcher(CharSequence regex, CharSequence text) {
return indexOfByRegexToMatcher(Pattern.compile(regex.toString()), text);
}
/**
* Index of using regex
*/
public static Matcher indexOfByRegexToMatcher(Pattern pattern, CharSequence text) {
Matcher m = pattern.matcher(text);
if ( m.find() ) {
return m;
}
return null;
}