Created
March 4, 2013 16:20
-
-
Save cy6erGn0m/5083426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cg; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
/** | |
* @author Sergey Mashkov (cy6erGn0m) | |
* @since 04.03.13 | |
*/ | |
public class JavaRegexpTest2 { | |
private final CharSequenceProxy seq; | |
private final List<String> patterns = new ArrayList<>(); | |
public JavaRegexpTest2(CharSequence input) { | |
this.seq = new CharSequenceProxy(input); | |
} | |
public static void main(String[] args) { | |
String input = "7778889990223344556677889900112244334433"; | |
new JavaRegexpTest2(input) | |
.findAll("[0123]123456|98765") | |
.printInfo(); | |
new JavaRegexpTest2(input) | |
.findAll("98765|[0123]123456|7") | |
.printInfo(); | |
new JavaRegexpTest2(input) | |
.findAll("[0123]123456") | |
.findAll("98765") | |
.printInfo(); | |
} | |
private void printInfo() { | |
for (String pattern : patterns) { | |
System.out.print(pattern); | |
System.out.print(" "); | |
} | |
System.out.print(" - "); | |
System.out.println(seq.getCounter()); | |
} | |
private JavaRegexpTest2 findAll(String pattern) { | |
findAll(Pattern.compile(pattern).matcher(seq)); | |
patterns.add(pattern); | |
return this; | |
} | |
private static void findAll(Matcher m) { | |
while (m.find()); | |
} | |
} | |
class CharSequenceProxy implements CharSequence { | |
private final CharSequence source; | |
private int counter = 0; | |
CharSequenceProxy(CharSequence source) { | |
this.source = source; | |
} | |
@Override | |
public int length() { | |
return source.length(); | |
} | |
@Override | |
public char charAt(int index) { | |
counter++; | |
return source.charAt(index); | |
} | |
@Override | |
public CharSequence subSequence(int start, int end) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String toString() { | |
return source.toString(); | |
} | |
public int getCounter() { | |
return counter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[0123]123456|98765 - 93 | |
98765|[0123]123456|7 - 142 | |
[0123]123456 98765 - 58 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment