An open API service indexing awesome lists of open source software.

https://github.com/teragrep/glb_01

Teragrep GlobToRegex Library for Java
https://github.com/teragrep/glb_01

Last synced: 3 months ago
JSON representation

Teragrep GlobToRegex Library for Java

Awesome Lists containing this project

README

          

= Teragrep GlobToRegex Library for Java

Teragrep GlobToRegex Library converts a https://en.wikipedia.org/wiki/Glob_(programming)[Glob pattern] into Java https://en.wikipedia.org/wiki/Regular_expression[regular expression].

== Features

Conversion supports following Glob expressions:
- Alternatives with nesting support (Braces)
- Character classes with negations (Brackets)
- Escapes (Backslashes)
- Wildcard characters (Questionmarks)
- Wildcard matches (Stars)
- Text into regex quotation

== Documentation

See the official documentation on https://docs.teragrep.com[docs.teragrep.com].

== Limitations

It is a pure Java implementation of following grammar and is bit hard to read.

[source,antlr4]
----

grammar Glob;

root // Glob.java
: rootSequence EOF
;

rootSequence // RootSequence.java
: (element | COMMA)*
;

element
: braceExp // BraceExpression.java
| charClass // CharacterClassExpression.java
| WILDCARD // WildcardExpression.java
| QUESTION // QuestionMarkExpression.java
| ESCAPED // EscapeExpression.java
| TEXT // Text.java
;

braceExp
: LBRACE element* (COMMA element*)* RBRACE
;

charClass
: LBRACK BANG? CLASS_CONTENT+ RBRACK
;

// --- Lexer Rules ---

ESCAPED : '\\' . ;
WILDCARD: '*' ;
QUESTION: '?' ;
LBRACK : '[' ;
RBRACK : ']' ;
LBRACE : '{' ;
RBRACE : '}' ;
COMMA : ',' ;
BANG : '!' ;

TEXT : ~[\\*?\[\]{},]+ ;
CLASS_CONTENT : ~[\]\\] | '\\' . ;
----

== How to [compile/use/implement]

=== Usage

[source,java]
----
import com.teragrep.glb_01.Glob;import com.teragrep.glb_01.GlobImpl;

Glob glob = new GlobImpl("test*Wildcard");
String regex = glob.asRegex();
System.out.println(regex); // Results into ^\Qtest\E.*\QWildcard\E$
----

== Contributing

// Change the repository name in the issues link to match with your project's name

You can involve yourself with our project by https://github.com/teragrep/glb_01/issues/new/choose[opening an issue] or submitting a pull request.

Contribution requirements:

. *All changes must be accompanied by a new or changed test.* If you think testing is not required in your pull request, include a sufficient explanation as why you think so.
. Security checks must pass
. Pull requests must align with the principles and http://www.extremeprogramming.org/values.html[values] of extreme programming.
. Pull requests must follow the principles of Object Thinking and Elegant Objects (EO).

Read more in our https://github.com/teragrep/teragrep/blob/main/contributing.adoc[Contributing Guideline].

=== Contributor License Agreement

Contributors must sign https://github.com/teragrep/teragrep/blob/main/cla.adoc[Teragrep Contributor License Agreement] before a pull request is accepted to organization's repositories.

You need to submit the CLA only once. After submitting the CLA you can contribute to all Teragrep's repositories.