Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/burnett01/kotlin-expression-builder
A regular expression (REGEX) class and builder (DSL), written in Kotlin.
https://github.com/burnett01/kotlin-expression-builder
builder dsl expression expressions java jvm kotlin package regex regular-expression
Last synced: 9 days ago
JSON representation
A regular expression (REGEX) class and builder (DSL), written in Kotlin.
- Host: GitHub
- URL: https://github.com/burnett01/kotlin-expression-builder
- Owner: Burnett01
- License: mit
- Archived: true
- Created: 2017-05-22T20:02:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-06T09:06:52.000Z (about 2 years ago)
- Last Synced: 2025-01-29T05:30:02.947Z (11 days ago)
- Topics: builder, dsl, expression, expressions, java, jvm, kotlin, package, regex, regular-expression
- Language: Kotlin
- Homepage:
- Size: 170 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# kotlin-expression-builder
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/Burnett01/paypal-basket/master/LICENSE)
[![](https://jitci.com/gh/Burnett01/kotlin-expression-builder/svg)](https://jitci.com/gh/Burnett01/kotlin-expression-builder)
[![Build Status](https://travis-ci.com/Burnett01/kotlin-expression-builder.svg?branch=master)](https://travis-ci.com/Burnett01/kotlin-expression-builder) [![](https://jitpack.io/v/burnett01/kotlin-expression-builder.svg)](https://jitpack.io/#burnett01/kotlin-expression-builder)
[![Scan with Detekt](https://github.com/Burnett01/kotlin-expression-builder/actions/workflows/detekt-analysis.yml/badge.svg)](https://github.com/Burnett01/kotlin-expression-builder/actions/workflows/detekt-analysis.yml)
An expression class and builder (DSL), written in Kotlin.
This package will assist you in writing and managing your regular expressions (Regex).
I've been using this package in my [HAFAS-parser](https://github.com/Burnett01/hafas-parser) project, in order to simplify very complex regular expressions. Read more about it [on my blog](https://burnett01.blogspot.de/2017/06/developers-should-ease-code-readability.html)
Features:
* Easy Regex management
* Available as a pure class (Expression)
* Available as a builder (ExpressionBuilder)
* Unit Tests---
# Table of contents
* [API Reference](#api-reference)
* [Expression](#expression)
* [ExpessionBuilder](#expressionbuilder)
* [Usage (ExpressionBuilder)](#usage-expressionbuilder)
* [Example](../master/contrib)
* [Setup / Install](#setup-install)
* [Build](#build)
* [Gradle](#gradle)
* [Unit-Tests](#unit-tests)
* [Gradle](#gradle-1)
* [CI](#ci)
* [Contributing](#contributing)
* [License](#license)---
## API Reference
### Expression:
```kotlin
class Expression( op: Set )fun markStart() = // '^'
fun markEnd() = // '$'
fun markOr() = // '|'/* Quantity (Q)
* Q.ZERO_OR_ONE = '?'
* Q.ZERO_OR_MORE = '*'
* Q.ONE_OR_MORE = '+'
*/
fun quantity(quant: Q = Q.ZERO_OR_ONE)/* Range
* Delimiter '-' = [min-max] (default)
* Delimiter ',' = {min,max}
*
* Example: range(0,9) = [0-9]
* Example: range(0,9,',') = {0,9}
* Example: range('A','z') = A-z
*/
fun range(min: Any, max: Any, delim: Char = '-')fun exact(times: Int) = // {times,times}
fun setChar(char: Char) = // char
fun setString(str: String) = // str
fun setLiteral(lit: Char) = // \lit
fun setDigit() = // \d
fun setWord() = // \wfun startMatch() = // [
fun endMatch() = // ]/* Start Capture Group
* 0 = capture -> (
* 1 = no capture -> (?:
*/
fun startGroup(type: Int)
fun endGroup() = // )fun debug()
fun compile(): Regex
```### ExpressionBuilder:
> For documentation check above (Expression class) or demos/examples.
```kotlin
class ExpressionBuilder { }
fun start()
fun end()
fun or()
fun quantity( quant: Q )
fun exact( times: Int )
fun range( min: Any, max: Any, delim: Char = '-' )
fun literal( lit: Char )
fun decimal( aggr: () -> Unit = {} )
fun string( str: String )
fun char( char: Char )
fun digit( aggr: () -> Unit = {} )
fun word( aggr: () -> Unit = {} )
fun whitespace()
fun match( aggr: () -> Unit = {} )
fun nocapture( aggr: () -> Unit )
fun capture( aggr: () -> Unit )fun expression(
entity: ExpressionBuilder.() -> Unit,
options: Set = emptySet()
): Expression?// In addition to the functions above,
// you can access every function of Expression class.
```---
## Usage (ExpressionBuilder):
**Demo 1:**
```kotlin
import com.github.burnett01.expression.*class main {
val date: String = "20.05.2017"
val origExpr = Regex("(\\d{2}.\\d{2}.\\d{4})")val myExpr: Expression? = expression {
capture {
digit { exact(2) }
literal('.')
digit { exact(2) }
literal('.')
digit { exact(4) }
}
}println(origExpr.find(date)?.value)
println(myExpr!!.compile().find(date)?.value)myExpr.debug()
}
```**Demo 2:**
```kotlin
import com.github.burnett01.expression.*class main {
val txt: String = "Hello catch me!%"
val myExpr: Expression? = expression({
capture {
string("Hello catch me!")
literal('%')
}
})println(myExpr!!.compile().find(txt)?.value)
myExpr.debug()
}
```**Passing options to Regex class:**
As of version 0.6, you may forward ``RegexOption``'s to the internal ``Regex`` class.
```kotlin
import com.github.burnett01.expression.*class main {
/* Available options (JVM):
IGNORE_CASE = Enables case-insensitive matching.
MULTILINE = Enables multiline mode.
LITERAL = Enables literal parsing of the pattern.
UNIX_LINES = In this mode, only the '\n' is recognized as a line terminator.
COMMENTS = Permits whitespace and comments in pattern.
DOT_MATCHES_ALL = Enables the mode, when the expression . matches any character,
including a line terminator.
CANON_EQ = Enables equivalence by canonical decomposition.
*/
val options: Set = setOf(RegexOption.IGNORE_CASE)val txt: String = "HElLo CaTCh mE!%"
val myExpr: Expression? = expression({
capture {
string("Hello catch me!")
literal('%')
}
}, options)println(myExpr.compile().find(txt)?.value)
myExpr!!.debug()
}
```---
## Setup / Install
Gradle dependency:
```kotlin
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}dependencies {
compile 'com.github.burnett01:kotlin-expression-builder:1.2.2'
}
```Maven dependency:
```kotlin
jitpack.io
https://jitpack.io
com.github.Burnett01
kotlin-expression-builder
1.2.2```
Check [here](https://jitpack.io/#Burnett01/kotlin-expression-builder/) for more.
---
## Build
### Gradle
```gradlew build```
---
## Unit-Tests
The testing-framework used by this package is [kotest](https://github.com/kotest/kotest).
Various tests are performed to make sure this package runs as smoothly as possible.
* src/test/kotlin/com/github/burnett01/expression/expressionTest.kt
`28 tests` | [Source](../master/src/test/kotlin/com/github/burnett01/expression/expressionTest.kt)
* src/test/kotlin/com/github/burnett01/expression/expression-builderTest.kt
`3 tests` | [Source](../master/src/test/kotlin/com/github/burnett01/expression/expression-builderTest.kt)
### Gradle
```gradlew test```
---
## CI
Travis-CI, jitCI and detekt (code QL)
---
## Contributing
You're very welcome and free to contribute. Thank you.
---
## License
[MIT](LICENSE)