in src/main/java/org/apache/commons/codec/language/bm/Rule.java [488:556]
private static RPattern pattern(final String regex) {
final boolean startsWith = regex.startsWith("^");
final boolean endsWith = regex.endsWith("$");
final String content = regex.substring(startsWith ? 1 : 0, endsWith ? regex.length() - 1 : regex.length());
final boolean boxes = content.contains("[");
if (!boxes) {
if (startsWith && endsWith) {
// exact match
if (content.isEmpty()) {
// empty
return input -> input.length() == 0;
}
return input -> input.equals(content);
}
if ((startsWith || endsWith) && content.isEmpty()) {
// matches every string
return ALL_STRINGS_RMATCHER;
}
if (startsWith) {
// matches from start
return input -> startsWith(input, content);
}
if (endsWith) {
// matches from start
return input -> endsWith(input, content);
}
} else {
final boolean startsWithBox = content.startsWith("[");
final boolean endsWithBox = content.endsWith("]");
if (startsWithBox && endsWithBox) {
String boxContent = content.substring(1, content.length() - 1);
if (!boxContent.contains("[")) {
// box containing alternatives
final boolean negate = boxContent.startsWith("^");
if (negate) {
boxContent = boxContent.substring(1);
}
final String bContent = boxContent;
final boolean shouldMatch = !negate;
if (startsWith && endsWith) {
// exact match
return input -> input.length() == 1 && contains(bContent, input.charAt(0)) == shouldMatch;
}
if (startsWith) {
// first char
return input -> input.length() > 0 && contains(bContent, input.charAt(0)) == shouldMatch;
}
if (endsWith) {
// last char
return input -> input.length() > 0 &&
contains(bContent, input.charAt(input.length() - 1)) == shouldMatch;
}
}
}
}
return new RPattern() {
final Pattern pattern = Pattern.compile(regex);
@Override
public boolean isMatch(final CharSequence input) {
final Matcher matcher = pattern.matcher(input);
return matcher.find();
}
};
}