Skip to content

Commit 4ee34e6

Browse files
zawatakilanwen
authored andcommitted
Add named-capturing group (#70)
* Add named-capturing group * Update coveralls-maven-plugin version * Add usage example for Builder#capture(String) and getText(String, String) * Fix local variable name to camelCase * Use assertThat instead of assertEquals
1 parent b11ef5d commit 4ee34e6

File tree

5 files changed

+251
-32
lines changed

5 files changed

+251
-32
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ before_install:
1212
- if [ ! -z "$GPG_OWNERTRUST" ]; then echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE --import-ownertrust; fi
1313

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

1717
notifications:
1818
email: false

src/main/java/ru/lanwen/verbalregex/VerbalExpression.java

+52-1
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,30 @@ public Builder oneOf(final String... pValues) {
578578
* @return this builder
579579
*/
580580
public Builder capture() {
581+
return this.capture(null);
582+
}
583+
584+
/**
585+
* Adds named-capture - open brace to current position and closed to suffixes
586+
* <p>
587+
* <pre>Example:{@code
588+
* String text = "[email protected]";
589+
* VerbalExpression regex = regex()
590+
* .find("@")
591+
* .capture("domain").anything().build();
592+
* regex.getText(text, "domain"); // => "example.com"
593+
* }</pre>
594+
*
595+
* @return this builder
596+
* @since 1.6
597+
*/
598+
public Builder capture(final String name) {
581599
this.suffixes.append(")");
582-
return this.add("(");
600+
601+
if (name == null || name.trim().isEmpty()) {
602+
return this.add("(");
603+
}
604+
return this.add("(?<" + name + ">");
583605
}
584606

585607
/**
@@ -592,6 +614,16 @@ public Builder capt() {
592614
return this.capture();
593615
}
594616

617+
/**
618+
* Shortcut for {@link #capture(String)}
619+
*
620+
* @return this builder
621+
* @since 1.6
622+
*/
623+
public Builder capt(final String name) {
624+
return this.capture(name);
625+
}
626+
595627
/**
596628
* Same as {@link #capture()}, but don't save result
597629
* May be used to set count of duplicated captures, without creating a new saved capture
@@ -716,6 +748,25 @@ public String getText(final String toTest, final int group) {
716748
return result.toString();
717749
}
718750

751+
/**
752+
* Extract exact named-group from string
753+
* <p>
754+
* Example is see to {@link Builder#capture(String)}
755+
*
756+
* @param toTest - string to extract from
757+
* @param group - group to extract
758+
* @return extracted group
759+
* @since 1.6
760+
*/
761+
public String getText(final String toTest, final String group) {
762+
Matcher m = pattern.matcher(toTest);
763+
StringBuilder result = new StringBuilder();
764+
while (m.find()) {
765+
result.append(m.group(group));
766+
}
767+
return result.toString();
768+
}
769+
719770
/**
720771
* Extract exact group from string and add it to list
721772
*

0 commit comments

Comments
 (0)