Helper function to split string into groups based on regexp. Possibility to retrieve...
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 15 Aug 2020 15:20:14 +0000 (18:20 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 15 Aug 2020 15:20:14 +0000 (18:20 +0300)
src/main/java/eu/svjatoslav/commons/string/String2.java
src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java
src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java

index af7ac27..07e3fdb 100755 (executable)
@@ -17,6 +17,9 @@ public class String2 {
         addSuffix(value);
     }
 
+    public String2() {
+    }
+
     public String2 repeat(int count){
         String value = toString();
 
@@ -56,6 +59,12 @@ public class String2 {
         return this;
     }
 
+    public String2 addSuffix(String s, int times) {
+        for (int i = 0; i < times; i++) addSuffix(s);
+        return this;
+    }
+
+
     /**
      * Cut given amount of characters from the left of the string.
      *
index 6aa56b4..14554be 100755 (executable)
@@ -94,7 +94,7 @@ public class Tokenizer {
 
             if (currentIndex >= source.length()) { // reached end of input
                 if (hasAccumulatedToken(tokenAccumulator))
-                    return new TokenizerMatch(tokenAccumulator.toString(), null, null);
+                    return new TokenizerMatch(tokenAccumulator.toString(), null, null, this);
                 else
                     return null;
             }
@@ -108,7 +108,7 @@ public class Tokenizer {
 
             if (matchResult.terminator.termination == PRESERVE) {
                 if (hasAccumulatedToken(tokenAccumulator))
-                    return new TokenizerMatch(tokenAccumulator.toString(), null, null);
+                    return new TokenizerMatch(tokenAccumulator.toString(), null, null, this);
 
                 currentIndex = matchResult.matcher.end();
                 return matchResult;
@@ -116,7 +116,7 @@ public class Tokenizer {
                 currentIndex = matchResult.matcher.end();
 
                 if (hasAccumulatedToken(tokenAccumulator))
-                    return new TokenizerMatch(tokenAccumulator.toString(), null, null);
+                    return new TokenizerMatch(tokenAccumulator.toString(), null, null, this);
             }
         }
     }
@@ -127,7 +127,7 @@ public class Tokenizer {
                 Matcher match = terminator.match(source, currentIndex);
                 if (match.find()) {
                     String token = source.substring(match.start(), match.end());
-                    return new TokenizerMatch(token, terminator, match);
+                    return new TokenizerMatch(token, terminator, match, this);
                 }
             }
         return null;
index 2b9b1f8..ebe5175 100755 (executable)
@@ -17,11 +17,13 @@ public class TokenizerMatch {
 
     public final Matcher matcher;
 
+    private Tokenizer tokenizer;
 
-    public TokenizerMatch(final String token, final Terminator terminator, Matcher matcher) {
+    public TokenizerMatch(final String token, final Terminator terminator, Matcher matcher, Tokenizer tokenizer) {
         this.token = token;
         this.terminator = terminator;
         this.matcher = matcher;
+        this.tokenizer = tokenizer;
     }
 
     public boolean isGroup(String group){
@@ -36,6 +38,16 @@ public class TokenizerMatch {
         return terminator.group.equals(group);
     }
 
+    public String[] getRegExpGroups(){
+        String[] result = new String[matcher.groupCount()];
+
+        for (int i = 0; i< result.length; i++){
+            result[i] = matcher.group(i+1);
+        }
+
+        return result;
+    }
+
     @Override
     public String toString() {
         return "TokenizerMatch{" +
@@ -43,4 +55,8 @@ public class TokenizerMatch {
                 ", terminator=" + terminator +
                 '}';
     }
+
+    public Tokenizer getTokenizer() {
+        return tokenizer;
+    }
 }