Java regular expressions made easy.

Overview

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/

Comments
  • maybe() method now accepts a builder as parameter

    maybe() method now accepts a builder as parameter

    I propose that all Builder methods which accept a string as parameter should have another version that accepts another Builder as parameter. This would allow users to break their expressions into smaller expressions, thus making their code more readable and maintainable. I built a test showing an example of an expression built with the oneOf() method being passed as the parameter for the maybe() method (with the current code this would not be possible, as the contents of the oneOf() expression would all be escaped with ).

    If you guys think this is a good idea, I will open another PR implementing the same idea for all other methods of the Builder class.

    opened by Tavio 13
  • Fixing the handling of (unmatched) optional capture groups

    Fixing the handling of (unmatched) optional capture groups

    I ran into an issue where I have a capture group defined, that is actually optional. Whenever that group cannot be matched (which is ok since it is optional) the regex.getText(testString, "optionalCaptureGroupName") call would return a string literal "null" instead of an empty string "".

    This PR provides a test showcasing the problem and a code adaption fixing the issue.

    opened by simschla 5
  • Added missing Macintosh line break

    Added missing Macintosh line break

    Macintosh line break was originally "\r\r". A so-called "universal" line-break matcher should match this as it can be encountered in real world.

    opened by joe-mojo 3
  • renaming anythingBut method name

    renaming anythingBut method name

    The expression "anything but ..." already means "anything except for ...". Adding "not" to the end of this expression is a double negation that may confuse users.

    opened by Tavio 2
  • Improved range function

    Improved range function

    Range function now works more like the JS version, accepting an array or varargs of strings. Also changed the loop logic thanks to @ryansmith94

    Fixes #9

    opened by dukky 2
  • lineBreak() has a capturing group

    lineBreak() has a capturing group

    Hello, The method lineBreak() contains a capturing group, which results in shifting of group numbers; the following test fails:

        final VerbalExpression expression = VerbalExpression.regex().
                            lineBreak()
                           .capture().find("some").endCapture().then(" text")
                           .build();
            assertEquals("some", expression.getText("\nsome text", 1));
    

    Cheers

    opened by masih 1
  • Javadoc ?

    Javadoc ?

    Is there some documentation (JavaDoc) available somewhere ? I do realize that the api is probably pretty obvious and self-documentimg, but some docs would be nice.

    opened by JonasCz 1
  • Bump bnd-maven-plugin from 6.3.1 to 6.4.0

    Bump bnd-maven-plugin from 6.3.1 to 6.4.0

    Bumps bnd-maven-plugin from 6.3.1 to 6.4.0.

    Release notes

    Sourced from bnd-maven-plugin's releases.

    Bnd/Bndtools 6.4.0

    Release Notes

    See Release Notes.

    What's Changed

    ... (truncated)

    Commits
    • 25e4898 build: 6.4.0 final build
    • bb7b8cf temp workspace was created in the wrong location
    • 90d324f build: Release 6.4.0-RC3
    • 67fbf14 maven: Support resolving bndrun files to an output folder
    • b9da98e maven: Fix bnd-run-maven-plugin bndrun file selection
    • 0c76117 maven: Move resolver earlier in reactor
    • 875a41f maven: Fix warnings from maven-plugin-plugin
    • 1da40ee gradle: Escape path to original bndrun file
    • 1a62b9f Revert "build(deps): Bump maven-deploy-plugin from 2.8.2 to 3.0.0 in /maven"
    • 164e101 build: Release 6.4.0-RC2
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 0
  • Bump maven-jar-plugin from 3.2.2 to 3.3.0

    Bump maven-jar-plugin from 3.2.2 to 3.3.0

    Bumps maven-jar-plugin from 3.2.2 to 3.3.0.

    Release notes

    Sourced from maven-jar-plugin's releases.

    3.3.0

    🚀 New features and improvements

    🐛 Bug Fixes

    📦 Dependency updates

    📝 Documentation updates

    • Restore mavenArchiverVersion property used in the site (#51) @​jorsol
    • (doc) Updated create-test-jar.apt.vm removing 'and' in Maven site Create Test JAR documentation (#34) @​focbenz

    👻 Maintenance

    Commits
    • d68df4b [maven-release-plugin] prepare release maven-jar-plugin-3.3.0
    • fb2299a Restore mavenArchiverVersion property used in the site
    • 1204127 [MJAR-290] - Update Plexus Utils to 3.4.2
    • 5fd2fc9 [MJAR-291] - Upgrade Parent to 37
    • 56344da use shared action v3 (#49)
    • 4148491 Code simplifications in AbstractMojo (#47)
    • 46c017d [MJAR-275] - Fix outputTimestamp not applied to module-info; breaks reproduci...
    • c02be20 [MJAR-278] Update plugin (requires Maven 3.2.5+) (#19)
    • b6fe3eb Bump junit from 4.11 to 4.13.2 in /src/it/MJAR-228
    • 78a28dd Ignore Maven Core updates
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 0
  • Bump maven-javadoc-plugin from 3.4.0 to 3.4.1

    Bump maven-javadoc-plugin from 3.4.0 to 3.4.1

    Bumps maven-javadoc-plugin from 3.4.0 to 3.4.1.

    Release notes

    Sourced from maven-javadoc-plugin's releases.

    3.4.1

    📦 Dependency updates

    Commits
    • a5db96e [maven-release-plugin] prepare release maven-javadoc-plugin-3.4.1
    • a10f0b1 [MJAVADOC-723] Upgrade Maven Reporting API to 3.1.1/Complete with Maven Repor...
    • c19dba2 Skip Java 9-14 in reproducible test
    • 26d84b2 Add notimestamp for reproducible builds test
    • 92ce668 Ignore Maven Core updates
    • bacc078 Add Integration Test for reproducible builds
    • 497f80f [MJAVADOC-719] - Update Maven Archiver to 3.6.0
    • 34b501d Bump assertj-core from 3.21.0 to 3.23.1
    • b928970 Bump spring-webmvc in /src/it/projects/MJAVADOC-434_fixcompile
    • 4306c92 Bump mockito-core from 4.1.0 to 4.4.0
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 0
  • How to get the startIndex and endIndex of match results?

    How to get the startIndex and endIndex of match results?

    I have to find all occurrences of dates in an email: each occurrence and the startIndex and endIndex of the occurrence. I would like to use a human-readable regex builder like JavaVerbalExpressions, but looking at the API I see no way to extract the match details like startIndex and endIndex, I only see test and testExact returning a boolean.

    Is it possible to loop over all matches and get the start and end indexes and all match details (group names etc)? Thanks!

    opened by josdejong 1
Releases(1.8)
  • 1.8(Mar 19, 2021)

    • Fixing the handling of (unmatched) optional capture groups (#75) by @simschla

      this change is breaking, as wouldn't return "null" as a string anymore - see tests of the PR for the details.

    Source code(tar.gz)
    Source code(zip)
  • 1.7(Aug 3, 2019)

  • 1.6(Oct 11, 2018)

  • 1.5(Dec 6, 2017)

  • v1.4(Jan 11, 2016)

  • v1.3(Jan 7, 2016)

    Improvements

    • #42 - new method List<String> getTextGroups(String toTest, int group) (resolves #28)
    • #39 - Ensure removeModifier method turns off the flags
    • #34 - maybe() method now accepts a builder as parameter
    • #29 - don't create additional capture when use OR

    Breaking changes

    • #33 - rename anythingButNot to anythingBut (remove double negation that may confuse users)
    Source code(tar.gz)
    Source code(zip)
  • v1.2(Jun 3, 2014)

    New features

    • #27 - lot of changes:
      • method to add another regex builder to current regex - use add(Builder) for it.
      • unnamed group method and shortcuts for capture* methods capt(), group() and so on...
      • oneOrMore(), zeroOrMore() and atLeast(int) methods

    Breaking changes:

    • non backward compatibility - make Builder constructor package-private.
      Use regex() factory method instead
    • non backward compatibility - change multiply(String, Integer...) method same as in original JS implementation

    Improvements

    • In tests:
      • replace assertTrue and assertFalse with assertThat with matchers
      • one more complex example (you can read on wiki page about it)
    Source code(tar.gz)
    Source code(zip)
  • v1.1(May 17, 2014)

    New features

    • #23 - capture, count methods

    • #25 - predefined character class methods

      \d    A digit: [0-9]
      \D    A non-digit: [^0-9]
      \s    A whitespace character: [ \t\n\x0B\f\r]
      \S    A non-whitespace character: [^\s]
      \w    A word character: [a-zA-Z_0-9]
      \W    A non-word character: [^\w]
      

    Improvements

    • #24 - javadoc and lot of tests
    Source code(tar.gz)
    Source code(zip)
  • v1.0(May 6, 2014)

Owner
null
"Pathfinder" - a small demo app made in Java, using Swing which shows an example implementation of a pathfinding algorithm based on BFS

"Pathfinder" is a small demo app made in Java, using Swing which shows an example implementation of a pathfinding algorithm based on BFS.

Dan Sirbu 2 Mar 9, 2022
Discord4J is a fast, powerful, unopinionated, reactive library to enable quick and easy development of Discord bots for Java, Kotlin, and other JVM languages using the official Discord Bot API.

Discord4J is a fast, powerful, unopinionated, reactive library to enable quick and easy development of Discord bots for Java, Kotlin, and other JVM languages using the official Discord Bot API.

null 1.5k Jan 4, 2023
hella-html is a library that makes it hella easy to generate dynamic HTML in vanilla Java.

Hella easy HTML in Java hella-html is a library that makes it hella easy to generate dynamic HTML in vanilla Java. Very lightweight and fast, the prim

null 1 Nov 23, 2022
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

Stfalcon LLC 31 Nov 29, 2021
Java lib for monitoring directories or individual files via java.nio.file.WatchService

ch.vorburger.fswatch Java lib for monitoring directories or individual files based on the java.nio.file.WatchService. Usage Get it from Maven Central

Michael Vorburger ⛑️ 21 Jan 7, 2022
Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) with quarterly updates. Tencent Kona JDK11 is certified as compatible with the Java SE standard.

Tencent Kona JDK11 Tencent Kona JDK11 is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-Term Support(LTS) w

Tencent 268 Dec 16, 2022
This repository contains Java programs to become zero to hero in Java.

This repository contains Java programs to become zero to hero in Java. Data Structure programs topic wise are also present to learn data structure problem solving in Java. Programs related to each and every concep are present from easy to intermidiate level

Sahil Batra 15 Oct 9, 2022
An open-source Java library for Constraint Programming

Documentation, Support and Issues Contributing Download and installation Choco-solver is an open-source Java library for Constraint Programming. Curre

null 607 Jan 3, 2023
Java Constraint Programming solver

https://maven-badges.herokuapp.com/maven-central/org.jacop/jacop/badge.svg [] (https://maven-badges.herokuapp.com/maven-central/org.jacop/jacop/) JaCo

null 202 Dec 30, 2022
Java Constraint Solver to solve vehicle routing, employee rostering, task assignment, conference scheduling and other planning problems.

OptaPlanner www.optaplanner.org Looking for Quickstarts? OptaPlanner’s quickstarts have moved to optaplanner-quickstarts repository. Quick development

KIE (Drools, OptaPlanner and jBPM) 2.8k Jan 2, 2023
Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas

Arthas Arthas is a Java Diagnostic tool open sourced by Alibaba. Arthas allows developers to troubleshoot production issues for Java applications with

Alibaba 31.5k Jan 4, 2023
Java rate limiting library based on token/leaky-bucket algorithm.

Java rate-limiting library based on token-bucket algorithm. Advantages of Bucket4j Implemented on top of ideas of well known algorithm, which are by d

Vladimir Bukhtoyarov 1.7k Jan 8, 2023
Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons

Project architect: @victornoel ATTENTION: We're still in a very early alpha version, the API may and will change frequently. Please, use it at your ow

Yegor Bugayenko 691 Dec 27, 2022
Dex : The Data Explorer -- A data visualization tool written in Java/Groovy/JavaFX capable of powerful ETL and publishing web visualizations.

Dex Dex : The data explorer is a powerful tool for data science. It is written in Groovy and Java on top of JavaFX and offers the ability to: Read in

Patrick Martin 1.3k Jan 8, 2023
Google core libraries for Java

Guava: Google Core Libraries for Java Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multi

Google 46.5k Jan 1, 2023
MinIO Client SDK for Java

MinIO Java SDK for Amazon S3 Compatible Cloud Storage MinIO Java SDK is Simple Storage Service (aka S3) client to perform bucket and object operations

High Performance, Kubernetes Native Object Storage 787 Jan 3, 2023
java port of Underscore.js

underscore-java Requirements Java 1.8 and later or Java 11. Installation Include the following in your pom.xml for Maven: <dependencies> <dependency

Valentyn Kolesnikov 411 Dec 6, 2022
(cross-platform) Java Version Manager

jabba Java Version Manager inspired by nvm (Node.js). Written in Go. The goal is to provide unified pain-free experience of installing (and switching

Stanley Shyiko 2.5k Jan 9, 2023
Manage your Java environment

Master your Java Environment with jenv Website : http://www.jenv.be Maintainers : Gildas Cuisinier Future maintainer in discussion: Benjamin Berman As

jEnv 4.6k Dec 30, 2022