Skip to content

Add named-capturing group #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
- if [ ! -z "$GPG_OWNERTRUST" ]; then echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE --import-ownertrust; fi

after_success:
- mvn clean cobertura:cobertura -Dcobertura.report.format=xml org.eluder.coveralls:coveralls-maven-plugin:3.0.1:report
- mvn clean cobertura:cobertura -Dcobertura.report.format=xml org.eluder.coveralls:coveralls-maven-plugin:4.3.0:report

notifications:
email: false
Expand Down
53 changes: 52 additions & 1 deletion src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,30 @@ public Builder oneOf(final String... pValues) {
* @return this builder
*/
public Builder capture() {
return this.capture(null);
}

/**
* Adds named-capture - open brace to current position and closed to suffixes
* <p>
* <pre>Example:{@code
* String text = "test@example.com";
* VerbalExpression regex = regex()
* .find("@")
* .capture("domain").anything().build();
* regex.getText(text, "domain"); // => "example.com"
* }</pre>
*
* @return this builder
* @since 1.6
*/
public Builder capture(final String name) {
this.suffixes.append(")");
return this.add("(");

if (name == null || name.trim().isEmpty()) {
return this.add("(");
}
return this.add("(?<" + name + ">");
}

/**
Expand All @@ -592,6 +614,16 @@ public Builder capt() {
return this.capture();
}

/**
* Shortcut for {@link #capture(String)}
*
* @return this builder
* @since 1.6
*/
public Builder capt(final String name) {
return this.capture(name);
}

/**
* Same as {@link #capture()}, but don't save result
* May be used to set count of duplicated captures, without creating a new saved capture
Expand Down Expand Up @@ -716,6 +748,25 @@ public String getText(final String toTest, final int group) {
return result.toString();
}

/**
* Extract exact named-group from string
* <p>
* Example is see to {@link Builder#capture(String)}
*
* @param toTest - string to extract from
* @param group - group to extract
* @return extracted group
* @since 1.6
*/
public String getText(final String toTest, final String group) {
Matcher m = pattern.matcher(toTest);
StringBuilder result = new StringBuilder();
while (m.find()) {
result.append(m.group(group));
}
return result.toString();
}

/**
* Extract exact group from string and add it to list
*
Expand Down
Loading