Skip to content

Commit

Permalink
Merge edb2a18 into b11ef5d
Browse files Browse the repository at this point in the history
  • Loading branch information
zawataki committed Oct 11, 2018
2 parents b11ef5d + edb2a18 commit a16901e
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
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
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

0 comments on commit a16901e

Please sign in to comment.