JavaVerbalExpressions

Java regular expressions made easy.

  • Owner: VerbalExpressions/JavaVerbalExpressions
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

JavaVerbalExpressions

release Maven Central

Coverage Status

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.5</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.").anythingBut(" ")
	           				.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
testRegex.getText(testString);    // returns: def

Builder can be cloned:

VerbalExpression regex = regex(regex().anything().addModifier('i')).endOfLine().build();

Or can be used in another regex:

VerbalExpression.Builder digits = regex().capt().digit().oneOrMore().endCapt().tab();
VerbalExpression regex2 = regex().add(digits).add(digits).build();

Feel free to use any predefined char groups:

regex().wordChar().nonWordChar()
   .space().nonSpace()
   .digit().nonDigit()

Define captures:

String text = "aaabcd";
VerbalExpression regex = regex()
                .find("a")
                .capture().find("b").anything().endCapture().then("cd").build();

regex.getText(text)     // returns "abcd"
regex.getText(text, 1)  // returns "b"

More complex examples

Other implementations

You can view all implementations on VerbalExpressions.github.io

[
Javascript -
PHP -
Python -
C# -
Objective-C -
Ruby -
Groovy -
Haskell -
C++ - ... (moarr) ]

Project released with travis

With help of this tutorial:
https://dracoblue.net/dev/uploading-snapshots-and-releases-to-maven-central-with-travis/

Main metrics

Overview
Name With OwnerVerbalExpressions/JavaVerbalExpressions
Primary LanguageJava
Program languageJava (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2013-07-25 16:15:26
Pushed At2025-10-01 04:36:48
Last Commit At2025-10-01 04:01:49
Release Count10
Last Release Name1.8 (Posted on )
First Release Namev1.0 (Posted on )
用户参与
Stargazers Count2.6k
Watchers Count97
Fork Count242
Commits Count149
Has Issues Enabled
Issues Count29
Issue Open Count10
Pull Requests Count69
Pull Requests Open Count5
Pull Requests Close Count12
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private