package us.codecraft.webmagic.selector;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Selector in regex.<br>
 *
 * @author code4crafter@gmail.com <br>
 * @since 0.1.0
 */
public class RegexSelector implements Selector {

    private String regexStr;

    private Pattern regex;

    private int group = 1;

    public RegexSelector(String regexStr, int group) {
        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);
        } catch (PatternSyntaxException e) {
            throw new IllegalArgumentException("invalid regex", e);
        }
        this.group = group;
    }

    public RegexSelector(String regexStr) {
        this(regexStr, 1);
    }

    private boolean hasGroup(String regexStr) {
        int x = StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(");
        int a = StringUtils.countMatches(regexStr, "(?:") - StringUtils.countMatches(regexStr, "\\(?:");
        int b = StringUtils.countMatches(regexStr, "(?=") - StringUtils.countMatches(regexStr, "\\(?=");
        int c = StringUtils.countMatches(regexStr, "(?<") - StringUtils.countMatches(regexStr, "\\(?<");
        int d = StringUtils.countMatches(regexStr, "(?!") - StringUtils.countMatches(regexStr, "\\(?!");
        int e = StringUtils.countMatches(regexStr, "(?#") - StringUtils.countMatches(regexStr, "\\(?#");
        
        if (x == (a + b + c + d + e)) {
            return false;
        }
        return true;
    }

    @Override
    public String select(String text) {
        return selectGroup(text).get(group);
    }

    @Override
    public List<String> selectList(String text) {
        List<String> strings = new ArrayList<String>();
        List<RegexResult> results = selectGroupList(text);
        for (RegexResult result : results) {
            strings.add(result.get(group));
        }
        return strings;
    }

    public RegexResult selectGroup(String text) {
        Matcher matcher = regex.matcher(text);
        if (matcher.find()) {
            String[] groups = new String[matcher.groupCount() + 1];
            for (int i = 0; i < groups.length; i++) {
                groups[i] = matcher.group(i);
            }
            return new RegexResult(groups);
        }
        return RegexResult.EMPTY_RESULT;
    }

    public List<RegexResult> selectGroupList(String text) {
        Matcher matcher = regex.matcher(text);
        List<RegexResult> resultList = new ArrayList<RegexResult>();
        while (matcher.find()) {
            String[] groups = new String[matcher.groupCount() + 1];
            for (int i = 0; i < groups.length; i++) {
                groups[i] = matcher.group(i);
            }
            resultList.add(new RegexResult(groups));
        }
        return resultList;
    }

    @Override
    public String toString() {
        return regexStr;
    }

}

@@ -664,16 +664,133 @@
         
+%0A        try %7B%0A            regex = Pattern.compile(regexStr, Pattern.DOTALL %7C Pattern.CASE_INSENSITIVE);%0A            
 // Check
@@ -857,16 +857,20 @@
  group.%0A
+    
         
@@ -976,33 +976,52 @@
     
+    
 if ( 
-! hasGroup(regexStr)
+regex.matcher(%22%22).groupCount() == 0
  )%7B%0A
@@ -1028,24 +1028,28 @@
             
+   
+ 
 regexStr = %22
@@ -1077,62 +1077,8 @@
     
-    %7D%0A        this.regexStr = regexStr;%0A        try %7B%0A
     
@@ -1155,32 +1155,84 @@
 E_INSENSITIVE);%0A
+            %7D%0A            this.regexStr = regexStr;%0A
         %7D catch 
@@ -1455,795 +1455,8 @@
  %7D%0A%0A
-    private boolean hasGroup(String regexStr) %7B%0A        int x = StringUtils.countMatches(regexStr, %22(%22) - StringUtils.countMatches(regexStr, %22%5C%5C(%22);%0A        int a = StringUtils.countMatches(regexStr, %22(?:%22) - StringUtils.countMatches(regexStr, %22%5C%5C(?:%22);%0A        int b = StringUtils.countMatches(regexStr, %22(?=%22) - StringUtils.countMatches(regexStr, %22%5C%5C(?=%22);%0A        int c = StringUtils.countMatches(regexStr, %22(?%3C%22) - StringUtils.countMatches(regexStr, %22%5C%5C(?%3C%22);%0A        int d = StringUtils.countMatches(regexStr, %22(?!%22) - StringUtils.countMatches(regexStr, %22%5C%5C(?!%22);%0A        int e = StringUtils.countMatches(regexStr, %22(?#%22) - StringUtils.countMatches(regexStr, %22%5C%5C(?#%22);%0A        %0A        if (x == (a + b + c + d + e)) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A
     

Update RegexSelector.java