Skip to content

Commit

Permalink
Merge pull request #10 from sgreben/named-capture-groups
Browse files Browse the repository at this point in the history
Support named capture groups
  • Loading branch information
sgreben authored May 5, 2020
2 parents 8824b3a + c0c6bcd commit b9891d3
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ There's a [discussion](https://www.reddit.com/r/java/comments/4tyk90/github_sgre
<dependency>
<groupId>com.github.sgreben</groupId>
<artifactId>regex-builder</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.sgreben</groupId>
<artifactId>regex-builder</artifactId>
<packaging>jar</packaging>
<version>1.1.0</version>
<version>1.2.0</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Construct regular expressions as pure Java code.</description>
<url>https://github.com/sgreben/regex-builder/</url>
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/github/sgreben/regex_builder/CaptureGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import com.github.sgreben.regex_builder.expression.Unary;
import com.github.sgreben.regex_builder.tokens.END_GROUP;
import com.github.sgreben.regex_builder.tokens.START_GROUP;
import com.github.sgreben.regex_builder.tokens.START_GROUP_NAMED;
import com.github.sgreben.regex_builder.tokens.TOKEN;

/**
* A regex capture group "(...)"
*/
public class CaptureGroup extends Unary {
private final String name;

public CaptureGroup(Expression expression) {
super(expression);
name = null;
}

public CaptureGroup(Expression expression, String name) {
super(expression);
this.name = name;
}

@Override
public void compile(CaptureGroupIndex index, java.util.List<TOKEN> output) {
output.add(new START_GROUP());
if (name != null) {
output.add(new START_GROUP_NAMED(name));
} else {
output.add(new START_GROUP());
}
for (Expression child : children()) {
child.compile(index, output);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/github/sgreben/regex_builder/FluentRe.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public CaptureGroup capture() {
return Re.capture(expression);
}

public CaptureGroup captureNamed(String name) {
return Re.captureNamed(name, expression);
}

public FluentRe separatedBy1(FluentRe e) {
return new FluentRe(Re.separatedBy1(e.expression, expression));
}
Expand Down Expand Up @@ -193,4 +197,8 @@ public FluentRe optional() {
public Pattern compile() {
return Pattern.compile(expression);
}
}

public Pattern compile(int flags) {
return Pattern.compile(expression, flags);
}
}
Loading

0 comments on commit b9891d3

Please sign in to comment.