From b1ffc7025cc976821987469570f07a7298ea16c9 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 15 Aug 2020 18:20:14 +0300 Subject: [PATCH] Helper function to split string into groups based on regexp. Possibility to retrieve Tokenizer from TokenizerMatch --- .../eu/svjatoslav/commons/string/String2.java | 9 +++++++++ .../commons/string/tokenizer/Tokenizer.java | 8 ++++---- .../string/tokenizer/TokenizerMatch.java | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index af7ac27..07e3fdb 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -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. * diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java index 6aa56b4..14554be 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java @@ -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; diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java index 2b9b1f8..ebe5175 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/TokenizerMatch.java @@ -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; + } } -- 2.20.1