VerbalExpressions is a Java library that helps to construct difficult regular expressions.
##Getting Started
Maven Dependency:
<dependency>
<groupId>ru.lanwen.verbalregex</groupId>
<artifactId>java-verbal-expressions</artifactId>
<version>1.0</version>
</dependency>
You can use SNAPSHOT dependency with adding to pom.xml
:
<repositories>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
##Examples
VerbalExpression testRegex = VerbalExpression.regex()
.startOfLine()
.then("http")
.maybe("s")
.then("://")
.maybe("www.")
.anythingButNot(" ")
.endOfLine()
.build();
// Create an example URL
String url = "https://www.google.com";
// Use VerbalExpression's testExact() method to test if the entire string matches the regex
testRegex.testExact(url); //True
testRegex.toString(); // Outputs the regex used:
// ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$
VerbalExpression testRegex = VerbalExpression.regex()
.startOfLine()
.then("abc")
.or("def")
.build();
String testString = "defzzz";
//Use VerbalExpression's test() method to test if parts if the string match the regex
testRegex.test(testString); //true
testRegex.testExact(testString); //false
Builder can be cloned:
// Produce: (.*)$
VerbalExpression regex = regex(regex().anything().addModifier('i')).endOfLine().build();
You can view all implementations on VerbalExpressions.github.io