Commit b6b991a0 authored by Yihua Huang's avatar Yihua Huang Committed by GitHub

Merge pull request #556 from zhuyuesut/master

增加对零宽断言的支持
parents f02f469c 9e1b7ed3
...@@ -26,14 +26,16 @@ public class RegexSelector implements Selector { ...@@ -26,14 +26,16 @@ public class RegexSelector implements Selector {
if (StringUtils.isBlank(regexStr)) { if (StringUtils.isBlank(regexStr)) {
throw new IllegalArgumentException("regex must not be empty"); throw new IllegalArgumentException("regex must not be empty");
} }
try {
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
// Check bracket for regex group. Add default group 1 if there is no group. // 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. // Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
if ( ! hasGroup(regexStr) ){ if ( regex.matcher("").groupCount() == 0 ){
regexStr = "(" + regexStr + ")"; regexStr = "(" + regexStr + ")";
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
} }
this.regexStr = regexStr; this.regexStr = regexStr;
try {
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
throw new IllegalArgumentException("invalid regex", e); throw new IllegalArgumentException("invalid regex", e);
} }
...@@ -44,30 +46,6 @@ public class RegexSelector implements Selector { ...@@ -44,30 +46,6 @@ public class RegexSelector implements Selector {
this(regexStr, 1); 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 @Override
public String select(String text) { public String select(String text) {
return selectGroup(text).get(group); return selectGroup(text).get(group);
......
...@@ -25,8 +25,8 @@ public class RegexSelectorTest { ...@@ -25,8 +25,8 @@ public class RegexSelectorTest {
@Test @Test
public void testRegexWithZeroWidthAssertions() { public void testRegexWithZeroWidthAssertions() {
String regex = "^.*(?=\\?)"; String regex = "^.*(?=\\?)(?!\\?yy)";
String source = "hello world?xxxx"; String source = "hello world?xx?yy";
RegexSelector regexSelector = new RegexSelector(regex); RegexSelector regexSelector = new RegexSelector(regex);
String select = regexSelector.select(source); String select = regexSelector.select(source);
Assertions.assertThat(select).isEqualTo("hello world"); Assertions.assertThat(select).isEqualTo("hello world");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment