Skip to content

Commit

Permalink
added range and anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
tbee committed Mar 8, 2023
1 parent 02c126d commit b8e6732
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ It becomes interesting when things are more complex, for example for a log line:

```java
// RegExp for this: 127.0.0.1 - - [21/Jul/2014:9:55:27 -0800] "GET /home.html HTTP/1.1" 200 2048
RegExpCore regExp = RegExp.of()
RegExp regExp = RegExp.of()
.group("ip", oneOrMore(nonWhitespace()))
.text(" ")
.group("client", oneOrMore(nonWhitespace()))
Expand Down Expand Up @@ -65,6 +65,6 @@ Just include a dependency in your project. For the latest version see [Maven cen
<dependency>
<groupId>org.tbee.regexpbuilder</groupId>
<artifactId>regexpbuilder</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>
```
16 changes: 2 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.tbee.regexpbuilder</groupId>
<artifactId>regexpbuilder</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down Expand Up @@ -51,23 +51,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -89,7 +77,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<configuration>
<doclint>all,-missing</doclint>
</configuration>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/tbee/regexpbuilder/RE.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public RegExp group(String name, RegExp regExp) {
static public RegExp text(String s) {
return RegExp.of().text(s);
}
static public RegExp range(String fromChar, String toChar) {
return RegExp.of().range(fromChar, toChar);
}

static public RegExp oneOf(RegExp regExp) {
return RegExp.of().oneOf(regExp);
Expand Down
159 changes: 152 additions & 7 deletions src/main/java/org/tbee/regexpbuilder/RegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static RegExp of() {


// -------------------------
// COMPLEX
// GROUP

private RegExp startGroup(String name) {
regExpString += "(";
Expand Down Expand Up @@ -66,71 +66,216 @@ public int indexOf(String name) {
// -------------------------
// PATTERN

public RegExp text(String match) {
regExpString += escape(match);
/**
* Match a specific text.
* Any characters with special meaning in regular expressions will be escape.
* @param s the text to match, it does not need to be (regexp) escaped.
* @return
*/
public RegExp text(String s) {
regExpString += escape(s);
return this;
}

/**
* Match a range of text.
* Any characters with special meaning in regular expressions will be escape.
* @param fromChar the beginning character
* @param toChar the beginning character
* @return
*/
public RegExp range(String fromChar, String toChar) {
if (fromChar.length() != 1 || toChar.length() != 1) {
throw new IllegalArgumentException("The character parameters must be exactly 1 in length");
}
regExpString += "[" + escape(fromChar) + "-" + escape(toChar) + "]";
return this;
}

/**
* Match one of the characters
* @param regExp
* @return
*/
public RegExp oneOf(RegExp regExp) {
regExpString += "[" + regExp.toString() + "]";
return this;
}
public RegExp oneOf(String match) {
return oneOf(RegExp.of().text(match));
/**
* Match one of the characters
* @param s
* @return
*/
public RegExp oneOf(String s) {
return oneOf(RegExp.of().text(s));
}

/**
* Match any character but the specified ones
* @param regExp
* @return
*/
public RegExp notOneOf(RegExp regExp) {
regExpString += "[^" + regExp.toString() + "]";
return this;
}
public RegExp notOneOf(String match) {
return notOneOf(RegExp.of().text(match));
/**
* Match any character but the specified ones
* @param s
* @return
*/
public RegExp notOneOf(String s) {
return notOneOf(RegExp.of().text(s));
}

/**
* These characters may be present, or not.
* @param regExp
* @return
*/
public RegExp optional(RegExp regExp) {
regExpString += regExp.toString() + "?";
return this;
}
/**
* These characters may be present, or not.
* @param s
* @return
*/
public RegExp optional(String s) {
return optional(RegExp.of().text(s));
}

/**
* These characters can not be present, or once, or many times
* @param regExp
* @return
*/
public RegExp zeroOrMore(RegExp regExp) {
regExpString += regExp.toString() + "*";
return this;
}

/**
* These characters can not be present, or once, or many times
* @param s
* @return
*/
public RegExp zeroOrMore(String s) {
return zeroOrMore(RegExp.of().text(s));
}

/**
* These characters must be present once, but can be many times
* @param regExp
* @return
*/
public RegExp oneOrMore(RegExp regExp) {
regExpString += regExp.toString() + "+";
return this;
}
/**
* These characters must be present once, but can be many times
* @param s
* @return
*/
public RegExp oneOrMore(String s) {
return oneOrMore(RegExp.of().text(s));
}

/**
* Match any of the blocks of characters
* @param regExps
* @return
*/
public RegExp anyOf(RegExp... regExps) {
boolean first = true;
for (RegExp regExp : regExps) {
regExpString += (first ? "" : "|") + regExp.toString();
first = false;
}
return this;
}
/**
* Match any of the blocks of characters
* @param ss
* @return
*/
public RegExp anyOf(String... ss) {
RegExp[] regExps = new RegExp[ss.length];
for (int i = 0; i < ss.length; i++) {
regExps[i] = RegExp.of().text(ss[i]);
}
return anyOf(regExps);
}
/**
* Match any of the blocks of characters
* @param objects must be a RegExp or otherwise it will be converted to a string
* @return
*/
public RegExp anyOf(Object... objects) {
RegExp[] regExps = new RegExp[objects.length];
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof RegExp regExp) {
regExps[i] = regExp;
}
else {
regExps[i] = RegExp.of().text(objects[i].toString());
}
}
return anyOf(regExps);
}

/**
* These characters are present N times
* @param regExp
* @return
*/
public RegExp occurs(int times, RegExp regExp) {
regExpString += regExp.toString() + "{" + times + "}";
return this;
}
/**
* These characters are present N times
* @param s
* @return
*/
public RegExp occurs(int times, String s) {
return occurs(times, RegExp.of().text(s));
}

/**
* These characters are present at least N times
* @param regExp
* @return
*/
public RegExp occursAtLeast(int times, RegExp regExp) {
regExpString += regExp.toString() + "{" + times + ",}";
return this;
}
/**
* These characters are present at least N times
* @param s
* @return
*/
public RegExp occursAtLeast(int times, String s) {
return occursAtLeast(times, RegExp.of().text(s));
}

/**
* These characters are present N to M times
* @param regExp
* @return
*/
public RegExp occursBetween(int minTimes, int maxTimes, RegExp regExp) {
regExpString += regExp.toString() + "{" + minTimes + "," + maxTimes + "}";
return this;
}
/**
* These characters are present N to M times
* @param s
* @return
*/
public RegExp occursBetween(int minTimes, int maxTimes, String s) {
return occursBetween(minTimes, maxTimes, RegExp.of().text(s));
}
Expand Down
18 changes: 17 additions & 1 deletion src/test/java/org/tbee/regexpbuilder/RegExpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public void exactTest() {
Assertions.assertEquals(2, countMatches(regExp.toMatcher("^foobar^foo")));
}

@Test
public void rangeTest() {
RegExp regExp = RegExp.of()
.range("0", "9");
Assertions.assertEquals("[0-9]", regExp.toString());
}

@Test
public void oneOfTest() {
RegExp regExp = RegExp.of()
Expand Down Expand Up @@ -83,6 +90,13 @@ public void oneOrMoreTest() {
Assertions.assertEquals(2, countMatches(regExp.toMatcher("[foobar[foo")));
}

@Test
public void anyOfTest() {
RegExp regExp = RegExp.of()
.anyOf("^aaa", "$bbb", "(ccc", digit().wordChar());
Assertions.assertEquals("\\^aaa|\\$bbb|\\(ccc|\\d\\w", regExp.toString());
}

@Test
public void groupTest() {
RegExp regExp = RegExp.of()
Expand Down Expand Up @@ -152,6 +166,7 @@ public void complexTest() {
@Test
public void apacheLogTest() {
RegExp regExp = RegExp.of()
.startOfLine()
.group("ip", oneOrMore(nonWhitespace()))
.text(" ")
.group("client", oneOrMore(nonWhitespace()))
Expand All @@ -170,7 +185,8 @@ public void apacheLogTest() {
.text("\" ")
.group("status", oneOrMore(digit()))
.whitespace()
.group("size", oneOrMore(digit()));
.group("size", oneOrMore(digit()))
.endOfLine();

// https://github.com/sgreben/regex-builder#examples
System.out.println(regExp.toString());
Expand Down

0 comments on commit b8e6732

Please sign in to comment.