Skip to content

Add word boundary (Fixed #68) #71

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 1 commit into from
Aug 3, 2019
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
19 changes: 19 additions & 0 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,25 @@ public Builder nonSpace() {
return this.add("(?:\\S)");
}

/**
* Add word boundary: \b
* <p>
* Example:
* <pre>{@code
* VerbalExpression regex = regex()
* .wordBoundary().find("abc").wordBoundary()
* .build();
* regex.test("a abc"); // true
* regex.test("a.abc"); // true
* regex.test("aabc"); // false
* }</pre>
*
* @return this builder
*/
public Builder wordBoundary() {
return this.add("(?:\\b)");
}


/*
--- / end of predefined character classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,17 @@ public void testListOfTextGroups() {
assertThat(groups1.get(0), equalTo("Hello"));
assertThat(groups1.get(1), equalTo("World"));
}

@Test
public void testWordBoundary() {
VerbalExpression regex = regex()
.capture()
.wordBoundary().then("o").word().oneOrMore().wordBoundary()
.endCapture()
.build();

assertThat(regex.getText("apple orange grape", 1), is("orange"));
assertThat(regex.test("appleorange grape"), is(false));
assertThat(regex.test("apple3orange grape"), is(false));
}
}