Skip to content

Commit 2afb034

Browse files
committed
add method for getting matched text
1 parent de00a86 commit 2afb034

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/main/java/VerbalExpression.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ public boolean testExact(String toTest) {
258258
public boolean test(String toTest) {
259259
return Pattern.compile(this.pattern, this.modifiers).matcher(toTest).find();
260260
}
261+
262+
public String getText(String toTest) {
263+
Matcher m = Pattern.compile(this.pattern).matcher(toTest);
264+
StringBuilder result = new StringBuilder();
265+
while (m.find()){
266+
result.append(m.group());
267+
}
268+
return result.toString();
269+
}
261270

262271
public String toString() {
263272
return this.pattern.toString();

src/test/java/BasicFunctionalityUnitTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.junit.Test;
22
import org.junit.Before;
33
import org.junit.Ignore;
4+
45
import static org.junit.Assert.*;
56

67
public class BasicFunctionalityUnitTests {
@@ -199,4 +200,16 @@ public void testSearchOneLine () {
199200
testString = "a\nb";
200201
assertTrue("b is on the second line but we are only searching the first", testRegex.test(testString));
201202
}
203+
204+
205+
@Test
206+
public void testGetText () {
207+
String testString = "123 https://www.google.com 456";
208+
VerbalExpression testRegex = new VerbalExpression().add("http")
209+
.maybe("s").then("://").then("www.").anythingBut(" ")
210+
.add("com");
211+
assertEquals(testRegex.getText(testString), "https://www.google.com");
212+
213+
}
214+
202215
}

0 commit comments

Comments
 (0)