mirror of
https://github.com/corda/corda.git
synced 2025-01-07 13:38:47 +00:00
d00f799d2e
Among other challenges, this regular expression is designed to demonstrate that thread prioritization is finicky: Given the string 'aaaaaa' to match, the first four threads will try to grab the second 'a', the third thread (the one that matched the '(a??)' group) having scheduled the same instruction pointer to the '(a+)' group that the second -- higher-priority -- thread will try to advance to only after processing the '(a??)' group's SPLIT. The second thread must override the third thread in that case, essentially stopping the latter. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
import regex.Matcher;
|
|
import regex.Pattern;
|
|
|
|
public class Regex {
|
|
private static void expect(boolean v) {
|
|
if (! v) throw new RuntimeException();
|
|
}
|
|
|
|
private static Matcher getMatcher(String regex, String string) {
|
|
return Pattern.compile(regex).matcher(string);
|
|
}
|
|
|
|
private static void expectMatch(String regex, String string) {
|
|
expect(getMatcher(regex, string).matches());
|
|
}
|
|
|
|
private static void expectNoMatch(String regex, String string) {
|
|
expect(!getMatcher(regex, string).matches());
|
|
}
|
|
|
|
private static void expectGroups(String regex, String string,
|
|
String... groups) {
|
|
Matcher matcher = getMatcher(regex, string);
|
|
expect(matcher.matches());
|
|
expect(matcher.groupCount() == groups.length);
|
|
for (int i = 1; i <= groups.length; ++i) {
|
|
expect(groups[i - 1].equals(matcher.group(i)));
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
expectMatch("a(bb)?a", "abba");
|
|
expectNoMatch("a(bb)?a", "abbba");
|
|
expectNoMatch("a(bb)?a", "abbaa");
|
|
expectGroups("a(a*?)(a?)(a??)(a+)(a*)a", "aaaaaa", "", "a", "", "aaa", "");
|
|
}
|
|
}
|