|
|
|
@ -28,8 +28,7 @@ public class RegexSelector implements Selector {
|
|
|
|
|
}
|
|
|
|
|
// 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 (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
|
|
|
|
|
StringUtils.countMatches(regexStr, "(?:") - StringUtils.countMatches(regexStr, "\\(?:")) {
|
|
|
|
|
if ( ! hasGroup(regexStr) ){
|
|
|
|
|
regexStr = "(" + regexStr + ")";
|
|
|
|
|
}
|
|
|
|
|
this.regexStr = regexStr;
|
|
|
|
@ -45,6 +44,30 @@ 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);
|
|
|
|
|