Skip to content
/ RegexApi Public

A Java DSL for the regular expression language.

License

Notifications You must be signed in to change notification settings

xmlet/RegexApi

Repository files navigation

Maven Central

RegexApi

This library is a Java DSL for the regular expression syntax. This library was created with the help of the xmlet infrastructure, using XsdAsmFaster. The classes generated were based in a manually created XML Schema Definition file, i.e. XSD file, which describes the whole regular expression syntax directly supported by Java. The names given to each operation were meant to make it easier to read and understand the regular expression operations. This library was created in order to simplify the usage and the interpretation of regular expressions.

Installation

First, in order to include it to your Maven project, simply add this dependency:

<dependency>
    <groupId>com.github.xmlet</groupId>
    <artifactId>regexApi</artifactId>
    <version>1.0.0</version>
</dependency>

How does RegexApi works?

This Regex DSL contains classes that represent all the regular expressions operations described in the Java 8 Pattern class documentation. The attributes of the expressions were converted to XSD attributes and the operations were converted to XSD elements. This results in the definition of regular expressions as shown in the following code snippet:
class Example{
    public void testFromFirstUntilLastRegex(){
        String toMatch = "abcd rXsXtXz";
        Regex regex = new Regex(expr -> expr.matchRegex()
                                .fromFirstUntilLast().attrFirst("a").attrLast("d")
                                .or()
                                .fromFirstUntilLast().attrFirst("r").attrLast("z"));
        
        List<String> result = regex.match(toMatch);

        Assert.assertEquals(8, result.size());
        Assert.assertEquals("a", result.get(0));
        Assert.assertEquals("b", result.get(1));
        Assert.assertEquals("c", result.get(2));
        Assert.assertEquals("d", result.get(3));
        Assert.assertEquals("r", result.get(4));
        Assert.assertEquals("s", result.get(5));
        Assert.assertEquals("t", result.get(6));
        Assert.assertEquals("z", result.get(7));
    }
}
The code presented above was extracted from the Regex project present in the CharacterClassesTest. In this concrete example we define a regular expression that states that will match:

  • fromFirstUntilLast().attrFirst("a").attrLast("d") - Any character between a and d.
  • or() - A match occurs either by a match happening in the previous expression or a match in the following expression.
  • fromFirstUntilLast().attrFirst("r").attrLast("z") - Any character between r and z.
This chaninig of method calls result in the following regular expression: [a-d]|[r-z]

About

A Java DSL for the regular expression language.

Resources

License

Stars

Watchers

Forks

Packages

No packages published