Skip to content

Commit 44af1c7

Browse files
zawatakilanwen
authored andcommitted
Add word boundary (Fixed #68) (#71)
1 parent 4ee34e6 commit 44af1c7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,25 @@ public Builder nonSpace() {
335335
return this.add("(?:\\S)");
336336
}
337337

338+
/**
339+
* Add word boundary: \b
340+
* <p>
341+
* Example:
342+
* <pre>{@code
343+
* VerbalExpression regex = regex()
344+
* .wordBoundary().find("abc").wordBoundary()
345+
* .build();
346+
* regex.test("a abc"); // true
347+
* regex.test("a.abc"); // true
348+
* regex.test("aabc"); // false
349+
* }</pre>
350+
*
351+
* @return this builder
352+
*/
353+
public Builder wordBoundary() {
354+
return this.add("(?:\\b)");
355+
}
356+
338357

339358
/*
340359
--- / end of predefined character classes

src/test/java/ru/lanwen/verbalregex/BasicFunctionalityUnitTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -770,4 +770,17 @@ public void testListOfTextGroups() {
770770
assertThat(groups1.get(0), equalTo("Hello"));
771771
assertThat(groups1.get(1), equalTo("World"));
772772
}
773+
774+
@Test
775+
public void testWordBoundary() {
776+
VerbalExpression regex = regex()
777+
.capture()
778+
.wordBoundary().then("o").word().oneOrMore().wordBoundary()
779+
.endCapture()
780+
.build();
781+
782+
assertThat(regex.getText("apple orange grape", 1), is("orange"));
783+
assertThat(regex.test("appleorange grape"), is(false));
784+
assertThat(regex.test("apple3orange grape"), is(false));
785+
}
773786
}

0 commit comments

Comments
 (0)