package developers.pocket.knife.ui.tools.regex;

import developers.pocket.knife.i18n.Messages;

import javax.inject.Inject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExModel {
    @Inject
    Messages messages;

    public String applyRegEx(String regEx, String testData) {
        StringBuilder sb = new StringBuilder();
        String lines[] = testData.split("\\r?\\n");
        for (String line : lines) {
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(line);
            int count = 0;
            while (matcher.find()) {
                count++;
                if (count > 1) {
                    sb.append(" ");
                }
                sb.append(count + ". match:");
                sb.append(line.substring(matcher.start(), matcher.end()));
                int groupCount = matcher.groupCount();
                for (int i = 1; i <= groupCount; i++) {
                    String group = matcher.group(i);
                    if(i == 1 && count >= 1) {
                        sb.append(" ");
                    }
                    if (i > 1) {
                        sb.append(" ");
                    }
                    sb.append(i + ". group:" + group);
                }
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

@@ -438,24 +438,60 @@
  : lines) %7B%0A
+            sb.append(line + %22%5Cn%22);%0A
             
@@ -694,95 +694,8 @@
     
-if (count %3E 1) %7B%0A                    sb.append(%22 %22);%0A                %7D%0A                
 sb.a
@@ -700,16 +700,23 @@
 .append(
+%22%5Ct%22 + 
 count + 
@@ -800,16 +800,23 @@
 r.end())
+ + %22%5Cn%22
 );%0A     
@@ -998,71 +998,53 @@
     
-if(i == 1 && count %3E= 1) %7B%0A                        sb.append(%22 
+sb.append(%22%5Ct%5Ct%22 + i + %22. group:%22 + group+%22%5Cn
 %22);%0A
@@ -1055,38 +1055,38 @@
             
+%7D%0A
     
-%7D%0A
         
             
@@ -1065,32 +1065,34 @@
   %7D%0A            
+%7D%0A
             if (
@@ -1093,27 +1093,23 @@
   if
- (i %3E 1) %7B%0A        
+(count == 0) %7B%0A
     
@@ -1135,150 +1135,38 @@
 nd(%22
- %22);%0A                    %7D%0A                    sb.append(i + %22. group:%22 + group);%0A                %7D%0A            %7D%0A            sb.append(%22%5Cn%22);
+%5CtNo match.%5Cn%22);%0A            %7D
 %0A   

improved format of output