|
|
|
@ -26,14 +26,16 @@ public class RegexSelector implements Selector {
|
|
|
|
|
if (StringUtils.isBlank(regexStr)) {
|
|
|
|
|
throw new IllegalArgumentException("regex must not be empty");
|
|
|
|
|
}
|
|
|
|
|
// Check bracket for regex group. Add default group 1 if there is no group.
|
|
|
|
|
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
|
|
|
|
|
if ( ! hasGroup(regexStr) ){
|
|
|
|
|
regexStr = "(" + regexStr + ")";
|
|
|
|
|
}
|
|
|
|
|
this.regexStr = regexStr;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
|
|
|
|
// Check bracket for regex group. Add default group 1 if there is no group.
|
|
|
|
|
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
|
|
|
|
|
if ( regex.matcher("").groupCount() == 0 ){
|
|
|
|
|
regexStr = "(" + regexStr + ")";
|
|
|
|
|
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
|
|
|
|
}
|
|
|
|
|
this.regexStr = regexStr;
|
|
|
|
|
} catch (PatternSyntaxException e) {
|
|
|
|
|
throw new IllegalArgumentException("invalid regex", e);
|
|
|
|
|
}
|
|
|
|
@ -44,30 +46,6 @@ public class RegexSelector implements Selector {
|
|
|
|
|
this(regexStr, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean hasGroup(String regexStr) {
|
|
|
|
|
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?:") - StringUtils.countMatches(regexStr, "\\(?:")){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?=") - StringUtils.countMatches(regexStr, "\\(?=") ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?<") - StringUtils.countMatches(regexStr, "\\(?<") ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?!") - StringUtils.countMatches(regexStr, "\\(?!") ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?#") - StringUtils.countMatches(regexStr, "\\(?#") ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String select(String text) {
|
|
|
|
|
return selectGroup(text).get(group);
|
|
|
|
|