Skip to content

Commit

Permalink
Support named capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed May 5, 2020
1 parent 8824b3a commit 22e1a78
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 80 deletions.
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 22e1a78

Please sign in to comment.