Skip to content

Commit

Permalink
Merge pull request #5945 from pkriens/issues/regex-corrections
Browse files Browse the repository at this point in the history
small corrections on RE
  • Loading branch information
pkriens committed Dec 24, 2023
2 parents f57fdf7 + f84e3dc commit 5524a45
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
74 changes: 64 additions & 10 deletions aQute.libg/src/aQute/libg/re/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
*/
public class Catalog {


/**
* If this class is extended, the named fields in that class can be used in
* named groups. This method will lookup the name of a field and create a
Expand Down Expand Up @@ -908,6 +909,8 @@ public static RE string(char delimeter) {
final public static RE empty = new REImpl("");
final public static C tab = new Special("\t");
final public static RE number = some(digit);
public static C hexdigit = cc("0-9A-F");
public static RE hexnumber = some(hexdigit);
final public static C minus = new CharacterClass("-");
final public static C dquote = new CharacterClass("\"");
final public static C squote = new CharacterClass("'");
Expand All @@ -923,6 +926,7 @@ public static RE string(char delimeter) {
final public static C javaJavaIdentifierStart = new Predefined("javaJavaIdentifierStart", true);
final public static C javaJavaIdentifierPart = new Predefined("javaJavaIdentifierPart", true);
final public static RE javaId = seq(javaJavaIdentifierStart, set(javaJavaIdentifierPart));
final public static RE fullyQualifiedName = seq(javaId, set(dot, javaId));
final public static RE startOfLine = new Boundary("^");
final public static RE endOfLine = new Boundary("$");
final public static RE wordBoundary = new Boundary("\\b");
Expand Down Expand Up @@ -1059,6 +1063,37 @@ public int end() {
return end < 0 ? end = matcher.start(name) : end;
}
}
class MatchGroupImplIndex extends Base implements MatchGroup {
final int name;
String value;
int start = -1;
int end = -1;

MatchGroupImplIndex(int name, String value) {
this.name = name;
this.value = value;
}

@Override
public String name() {
return Integer.toString(name);
}

@Override
public String value() {
return value == null ? value : matcher.group(name);
}

@Override
public int start() {
return start < 0 ? start = matcher.start(name) : start;
}

@Override
public int end() {
return end < 0 ? end = matcher.end(name) : end;
}
}
class MatchImpl extends Base implements Match {
Map<String, MatchGroup> matchGroups;
Map<String, String> matchValues;
Expand Down Expand Up @@ -1089,15 +1124,17 @@ public Map<String, MatchGroup> getGroups() {
if (matchGroups == null) {
if (groups == null)
matchGroups = Collections.emptyMap();
Map<String, MatchGroup> result = new TreeMap<>();
for (String name : groups) {
String value = matcher.group(name);
if (value != null) {
MatchGroupImpl mg = new MatchGroupImpl(name, value);
result.put(name, mg);
else {
Map<String, MatchGroup> result = new TreeMap<>();
for (String name : groups) {
String value = matcher.group(name);
if (value != null) {
MatchGroupImpl mg = new MatchGroupImpl(name, value);
result.put(name, mg);
}
}
matchGroups = Collections.unmodifiableMap(result);
}
matchGroups = Collections.unmodifiableMap(result);
}
return matchGroups;
}
Expand Down Expand Up @@ -1138,6 +1175,19 @@ public String tryMatch(RE expected) {
return null;
}

@Override
public Optional<MatchGroup> group(int group) {

if (matcher.groupCount() < group)
return Optional.empty();

String value = matcher.group(group);
if (value == null)
return Optional.empty();

return Optional.of(new MatchGroupImplIndex(group, value));
}

}
return Optional.of(new MatchImpl());
} else
Expand Down Expand Up @@ -1501,11 +1551,11 @@ static class Option extends REImpl implements F {
final EnumSet<Flag> negative;

Option(EnumSet<Flag> positive, EnumSet<Flag> negative, RE... res) {
this(toGroupedString(false, res), positive, negative);
this(toGroupedString(false, res), positive, negative, names(res));
}

Option(String ungrouped, EnumSet<Flag> p, EnumSet<Flag> n) {
super(ungrouped);
Option(String ungrouped, EnumSet<Flag> p, EnumSet<Flag> n, String... names) {
super(ungrouped, names);
this.positive = p == null ? NONE_OF : p;
this.negative = n == null ? NONE_OF : n;
}
Expand Down Expand Up @@ -1721,4 +1771,8 @@ static boolean isWhiteSpace(RE re) {
};
}

public static RE re(String regex) {
return new REImpl(regex);
}

}
2 changes: 2 additions & 0 deletions aQute.libg/src/aQute/libg/re/RE.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ default boolean check(RE expected) {
* @param match the RE to match return a string when matched or null
*/
String tryMatch(RE match);

Optional<MatchGroup> group(int group);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions aQute.libg/test/aQute/libg/re/RETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ public void testNamed() {
assertThat(test3.toString()).isEqualTo("(?:(?<foo>a)(?<bar>(?<xyz>b)))");
assertThat(test3.getGroupNames()).containsExactlyInAnyOrder("foo", "bar", "xyz");

RE testOption = Catalog.caseInsenstive(test, test2, test3);
assertThat(testOption.getGroupNames()).containsExactlyInAnyOrder("foo", "bar", "xyz");

}

@Test
Expand Down

0 comments on commit 5524a45

Please sign in to comment.