Skip to content

Commit 9828065

Browse files
committed
Merge pull request #31 from Tavio/one_of
adding oneOf method
2 parents 85d92e1 + c812ccd commit 9828065

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
target
22
.idea
33
*.iml
4+
.classpath
5+
.project
6+
.settings
7+
/target/
8+
bin

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,30 @@ public Builder or(final String pValue) {
552552
}
553553
return this;
554554
}
555+
556+
/**
557+
* Adds an alternative expression to be matched
558+
* based on an array of values
559+
*
560+
* @param pValues - the strings to be looked for
561+
* @return this builder
562+
*/
563+
public Builder oneOf(final String... pValues) {
564+
if(pValues != null && pValues.length > 0) {
565+
this.add("(?:");
566+
for(int i = 0; i < pValues.length; i++) {
567+
String value = pValues[i];
568+
this.add("(?:");
569+
this.add(value);
570+
this.add(")");
571+
if(i < pValues.length - 1) {
572+
this.add("|");
573+
}
574+
}
575+
this.add(")");
576+
}
577+
return this;
578+
}
555579

556580
/**
557581
* Adds capture - open brace to current position and closed to suffixes

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,45 @@ public void zeroOreMoreSameAsAtLeast0() throws Exception {
538538
assertThat(regexWithOneOrMore, matchesTo(empty));
539539
assertThat(regexWithOneOrMore, matchesExactly(empty));
540540
}
541+
542+
@Test
543+
public void testOneOf() {
544+
VerbalExpression testRegex = new VerbalExpression.Builder()
545+
.startOfLine()
546+
.oneOf("abc", "def")
547+
.build();
548+
549+
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
550+
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
551+
assertThat("Doesn't start with abc nor def", testRegex, not(matchesTo("xyzabc")));
552+
}
553+
554+
@Test
555+
public void testOneOfWithCapture() {
556+
VerbalExpression testRegex = regex()
557+
.capture()
558+
.oneOf("abc", "def")
559+
.build();
560+
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
561+
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
562+
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
563+
564+
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcdef"));
565+
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("def"));
566+
}
567+
568+
@Test
569+
public void testOneOfWithClosedCapture() {
570+
VerbalExpression testRegex = regex()
571+
.capture()
572+
.oneOf("abc", "def")
573+
.endCapt()
574+
.build();
575+
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
576+
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
577+
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
578+
579+
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcdef"));
580+
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("def"));
581+
}
541582
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import org.junit.Ignore;
44
import org.junit.Test;
55

6-
76
import static org.hamcrest.CoreMatchers.equalTo;
8-
import static org.junit.Assert.assertThat;
7+
import static org.junit.Assert.*;
98
import static ru.lanwen.verbalregex.VerbalExpression.regex;
109
import static ru.lanwen.verbalregex.matchers.TestMatchMatcher.matchesTo;
1110
import static ru.lanwen.verbalregex.matchers.TestsExactMatcher.matchesExactly;
@@ -108,4 +107,15 @@ public void unusualRegex() throws Exception {
108107
@Ignore("Planned in 1.3")
109108
public void captureWithName() throws Exception {
110109
}
110+
111+
@Test
112+
public void testStarWarsMovies() {
113+
VerbalExpression regex = VerbalExpression.regex()
114+
.find("Star Wars: ")
115+
.oneOf("The Phantom Menace", "Attack of the Clones", "Revenge of the Sith",
116+
"The Force Awakens", "A New Hope", "The Empire Strikes Back", "Return of the Jedi")
117+
.build();
118+
assertTrue(regex.test("Star Wars: The Empire Strikes Back"));
119+
assertTrue(regex.test("Star Wars: Return of the Jedi"));
120+
}
111121
}

0 commit comments

Comments
 (0)