Skip to content

senocak/regexpbuilderkotlin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

 ____            _____            ____        _ _     _          
|  _ \ ___  __ _| ____|_  ___ __ | __ ) _   _(_) | __| | ___ _ __
| |_) / _ \/ _` |  _| \ \/ / '_ \|  _ \| | | | | |/ _` |/ _ \ '__
|  _ <  __/ (_| | |___ >  <| |_) | |_) | |_| | | | (_| |  __/ |  
|_| \_\___|\__, |_____/_/\_\ .__/|____/ \__,_|_|_|\__,_|\___|_|  
           |___/           |_|                                   

human-readable regular expressions for Jvm

release phpunit release release

RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.

Installation

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.senocak</groupId>
    <artifactId>regexpbuilderkotlin</artifactId>
    <version>1.0.1</version>
</dependency>

Or download the appropriate release and require RegExpBuilder.kt and RegExp.kt

Examples

Validation

val regExp1: RegExp = RegExpBuilder()
        .startOfInput()
        .startOfInput()
        .exactly(n = 4).digits()
        .then(s = "_")
        .exactly(n = 2).digits()
        .then(s = "_")
        .min(n = 3).max(n = 10).letters()
        .then(s = ".")
        .anyOf(r = listOf("png", "jpg", "gif"))
        .endOfInput()
        .getRegExp()
        .also { it: RegExp ->
            // True
            println(it.matches(inputString0 = "2020_10_hund.jpg"))
            println(it.matches(inputString0 = "2030_11_katze.png"))
            println(it.matches(inputString0 = "4000_99_maus.gif"))
            // False
            println(it.matches(inputString0 = "123_00_nein.gif"))
            println(it.matches(inputString0 = "4000_0_nein.pdf"))
            println(it.matches(inputString0 = "201505_nein.jpg"))
        }

Search

val regExp2: RegExp = RegExpBuilder()
    .multiLine()
    .globalMatch()
    .min(n = 1).max(n = 10).anythingBut(s = " ")
    .anyOf(r = listOf(".pdf", ".doc"))
    .getRegExp()
    .also { it: RegExp ->
        val text = """Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy SomeFile.pdf eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua. At vero eos et accusam lorem
et justo duo dolores et ea rebum. doc_04.pdf Stet clita kasd File.doc."""
        val matches: List<String> = it.findIn(inputString0 = text)
        println(matches[0] == "SomeFile.pdf")
        println(matches[1] == "doc_04.pdf")
        println(matches[2] == "File.doc")
    }

Replace

val regExp3: RegExp = RegExpBuilder()
    .min(n = 1)
    .max(n = 10)
    .digits()
    .getRegExp()
    .also { it: RegExp ->
        var text = "98 bottles of beer on the wall"
        text = it.replace(inputString0 = text) { match: String -> (match.toInt() + 1).toString() }
        println("99 bottles of beer on the wall" == text)

Validation with multiple patterns

val regExp41: RegExpBuilder = RegExpBuilder()
    .startOfInput()
    .exactly(n = 3).digits()
    .anyOf(r = listOf(".pdf", ".doc"))
    .endOfInput()

val regExp42: RegExpBuilder = RegExpBuilder()
    .getNew()
    .startOfInput()
    .exactly(n = 4).letters()
    .then(s = ".jpg")
    .endOfInput()

val regExp43: RegExp = RegExpBuilder()
    .getNew()
    .eitherFind(r = regExp41)
    .orFind(r = regExp42)
    .getRegExp()

//true
println(regExp43.matches(inputString0 = "123.pdf"))
println(regExp43.matches(inputString0 = "456.doc"))
println(regExp43.matches(inputString0 = "bbbb.jpg"))
println(regExp43.matches(inputString0 = "aaaa.jpg"))

//false
println(regExp43.matches(inputString0 = "1234.pdf"))
println(regExp43.matches(inputString0 = "123.gif"))
println(regExp43.matches(inputString0 = "aaaaa.jpg"))
println(regExp43.matches(inputString0 = "456.docx"))

Take a look at the tests for more examples