Giter Club home page Giter Club logo

javaverbalexpressions's Introduction

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.4</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) ]

javaverbalexpressions's People

Contributors

lanwen avatar es avatar tavio avatar mihai-vlc avatar jcputney avatar devnied avatar gufengwyx8 avatar go-oleg avatar tdmitriy avatar joe-mojo avatar msknapp avatar masih avatar dukky avatar seblm avatar vyacheslav-pushkin avatar bryant1410 avatar pathikrit avatar myjian avatar maurodec avatar jvimr avatar mgramin avatar

Watchers

James Cloos avatar Mike avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.