├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── kotlin │ └── com │ └── github │ └── h0tk3y │ └── regexDsl │ └── RegexDsl.kt └── test └── kotlin └── com └── github └── h0tk3y └── regexDsl └── RegexDslTest.kt /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Gradle template 3 | .gradle 4 | build/ 5 | 6 | # Ignore Gradle GUI config 7 | gradle-app.setting 8 | 9 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 10 | !gradle-wrapper.jar 11 | 12 | # Cache of project 13 | .gradletasknamecache 14 | 15 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 16 | # gradle/wrapper/gradle-wrapper.properties 17 | ### JetBrains template 18 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 19 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 20 | 21 | /.idea/* 22 | 23 | ## File-based project format: 24 | *.iws 25 | 26 | ## Plugin-specific files: 27 | 28 | # IntelliJ 29 | /out/ 30 | 31 | # mpeltonen/sbt-idea plugin 32 | .idea_modules/ 33 | 34 | # JIRA plugin 35 | atlassian-ide-plugin.xml 36 | 37 | # Crashlytics plugin (for Android Studio and IntelliJ) 38 | com_crashlytics_export_strings.xml 39 | crashlytics.properties 40 | crashlytics-build.properties 41 | fabric.properties 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # regex-dsl 2 | A Kotlin DSL for regular expressions 3 | 4 | ![Logo](https://dl.dropbox.com/s/v9cqloqxsior71w/regex_dsl_1.png) 5 | 6 | [![Release](https://jitpack.io/v/h0tk3y/regex-dsl.svg)](https://jitpack.io/#h0tk3y/regex-dsl) 7 | ![Kotlin version](https://img.shields.io/badge/kotlin-1.0.4-blue.svg) 8 | 9 | Provides a Groovy-style builder for regex with safe quantifiers and groups. Readable and extensible. 10 | 11 | ## Gradle dependency 12 | 13 | repositories { 14 | ... 15 | maven { url "https://jitpack.io" } 16 | } 17 | 18 | dependencies { 19 | compile 'com.github.h0tk3y:regex-dsl:v0.1' 20 | } 21 | 22 | ## Currently supports: 23 | 24 | * `literally("text")` that escapes the `text` 25 | * `anyChar()`, `digit()`, `letter()`, `alphaNumeric()`, `whitespace()`, `wordBoundary()`, `wordCharacter()` 26 | * `startOfString()`, `endOfString()` 27 | * `optional("text")` and `optional { /* inner regex */ }`, 28 | 29 | `oneOrMore("text")` and `oneOrMore { /* inner regex */ }`, 30 | 31 | `zeroOrMore("text")` and `zeroOrMore { /* inner regex */ }` 32 | 33 | * `n times ("text")` and `n times { /* inner regex */ }`, 34 | 35 | `n timesOrMore ("text")` and `n timesOrMore { /* inner regex */ }`, 36 | 37 | `n timesOrLess ("text")` and `n timesOrLess { /* inner regex */ }`, 38 | 39 | `n..m times ("text")` and `n..m times { /* inner regex */ }` 40 | 41 | * `index = group { /* inner regex */ }` and `matchGroup(index)`, 42 | 43 | `group("name") { /* inner regex */ }` and `matchGroup("name")` 44 | 45 | * `anyOf('a', 'b', 'c')`, 46 | 47 | `anyOf('a'..'z', 'A'..'Z', '0'..'9')`, 48 | 49 | `anyOf("abc", "def", "xyz")`, 50 | 51 | `anyOf({ /* inner regex*/ }, { /* inner regex *? })` 52 | 53 | * `include(anotherRegex)` 54 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.0.4' 3 | 4 | repositories { 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9 | } 10 | } 11 | 12 | apply plugin: 'kotlin' 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 20 | testCompile "junit:junit:4.12" 21 | } 22 | 23 | task sourcesJar(type: Jar, dependsOn: classes) { 24 | classifier = 'sources' 25 | from sourceSets.main.allSource 26 | } 27 | 28 | artifacts { 29 | archives sourcesJar 30 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/h0tk3y/regex-dsl/3175241346cc22c1096b88158cdccdfdd3e2eb4a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 10 17:43:05 MSK 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'regex-dsl' 2 | 3 | -------------------------------------------------------------------------------- /src/main/kotlin/com/github/h0tk3y/regexDsl/RegexDsl.kt: -------------------------------------------------------------------------------- 1 | package com.github.h0tk3y.regexDsl 2 | 3 | import java.util.regex.Pattern 4 | 5 | class RegexContext internal constructor(var lastGroup: Int = 0) { 6 | internal val regexParts = StringBuilder() 7 | 8 | private fun addPart(part: String) { 9 | regexParts.append(part) 10 | } 11 | 12 | fun anyChar() = addPart(".") 13 | 14 | fun digit() = addPart("\\d") 15 | 16 | fun letter() = addPart("[[:alpha:]]") 17 | 18 | fun alphaNumeric() = addPart("[A-Za-z0-9]") 19 | 20 | fun whitespace() = addPart("\\s") 21 | 22 | fun wordBoundary() = addPart("\\b") 23 | 24 | fun wordCharacter() = addPart("\\w") 25 | 26 | fun startOfString() = addPart("^") 27 | fun endOfString() = addPart("$") 28 | 29 | fun literally(s: String) = addPart(Regex.escape(s)) 30 | 31 | private fun addWithModifier(s: String, modifier: String) = addPart("(?:$s)$modifier") 32 | 33 | private fun pattern(block: RegexContext.() -> Unit): String { 34 | val innerContext = RegexContext(lastGroup) 35 | innerContext.block() 36 | lastGroup = innerContext.lastGroup 37 | return innerContext.regexParts.toString() 38 | } 39 | 40 | fun oneOrMore(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "+") 41 | fun oneOrMore(s: String) = oneOrMore { literally(s) } 42 | 43 | fun optional(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "?") 44 | fun optional(s: String) = optional { literally(s) } 45 | 46 | fun zeroOrMore(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "*") 47 | fun zeroOrMore(s: String) = zeroOrMore { literally(s) } 48 | 49 | infix fun Int.times(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "{$this}") 50 | infix fun Int.times(s: String) = this times { literally(s) } 51 | 52 | infix fun IntRange.times(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "{$first,$last}") 53 | infix fun IntRange.times(s: String) = this times { literally(s) } 54 | 55 | infix fun Int.timesOrMore(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "{$this,}") 56 | infix fun Int.timesOrMore(s: String) = this timesOrMore { literally(s) } 57 | 58 | infix fun Int.timesOrLess(block: RegexContext.() -> Unit) = addWithModifier(pattern(block), "{0,$this}") 59 | infix fun Int.timesOrLess(s: String) = this timesOrLess { literally(s) } 60 | 61 | fun group(block: RegexContext.() -> Unit): Int { 62 | val result = ++lastGroup 63 | addPart("(${pattern(block)})") 64 | return result 65 | } 66 | 67 | fun group(name: String, block: RegexContext.() -> Unit): Int { 68 | val result = ++lastGroup 69 | addPart("(?<$name>${pattern(block)})") 70 | return result 71 | } 72 | 73 | fun matchGroup(index: Int) = addPart("\\$index") 74 | fun matchGroup(name: String) = addPart("\\k<$name>") 75 | 76 | fun include(regex: Regex) { 77 | val pattern = regex.pattern 78 | addPart(pattern) 79 | lastGroup += Pattern.compile(pattern).matcher("").groupCount() 80 | } 81 | 82 | fun anyOf(vararg terms: String) = addPart(terms.joinToString("|", "(?:", ")") { Regex.escape(it) }) 83 | 84 | fun anyOf(vararg blocks: RegexContext.() -> Unit) = 85 | addPart(blocks.joinToString("|", "(?:", ")") { pattern(it) }) 86 | 87 | fun anyOf(vararg characters: Char) = 88 | addPart(characters.joinToString("", "[", "]").replace("\\", "\\\\").replace("^", "\\^")) 89 | 90 | fun anyOf(vararg ranges: CharRange) = addPart(ranges.joinToString("", "[", "]") { "${it.first}-${it.last}" }) 91 | } 92 | 93 | fun regex(block: RegexContext.() -> Unit): Regex { 94 | val context = RegexContext() 95 | context.block() 96 | val pattern = context.regexParts.toString() 97 | return Regex(pattern) 98 | } -------------------------------------------------------------------------------- /src/test/kotlin/com/github/h0tk3y/regexDsl/RegexDslTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.h0tk3y.regexDsl 2 | 3 | import org.junit.Assert.* 4 | import org.junit.Test 5 | 6 | class RegexDslTest { 7 | @Test fun literals() { 8 | val s = ".:!([pattern])!:." 9 | val r = regex { 10 | literally(s) 11 | } 12 | 13 | assertTrue(r in s) 14 | } 15 | 16 | @Test fun quantifiers() { 17 | val s = "test" 18 | val testRange = 0..5 19 | 20 | fun testRepeats(r: Regex, rule: (Int) -> Boolean) { 21 | for (i in testRange) { 22 | val text = s.repeat(i) 23 | assertEquals("${r.pattern}, $text", rule(i), r.matchEntire(text) != null) 24 | } 25 | } 26 | 27 | testRepeats(regex { optional(s) }, { it in 0..1 }) 28 | testRepeats(regex { oneOrMore(s) }, { it >= 1 }) 29 | testRepeats(regex { zeroOrMore(s) }, { it >= 0 }) 30 | testRepeats(regex { 3 times s }, { it == 3 }) 31 | testRepeats(regex { 4 timesOrMore s }, { it >= 4 }) 32 | testRepeats(regex { 2 timesOrLess s }, { it <= 2 }) 33 | testRepeats(regex { 2..3 times s }, { it in 2..3 }) 34 | } 35 | 36 | @Test fun nestedRegex() { 37 | val r = regex { 38 | optional { anyChar() } 39 | 2 times { literally("abc") } 40 | } 41 | 42 | assertTrue(r in "xabcabc") 43 | assertTrue(r in "abcabc") 44 | } 45 | 46 | @Test fun specials() { 47 | val r = regex { 48 | anyChar() 49 | digit() 50 | wordCharacter() 51 | wordBoundary() 52 | whitespace() 53 | letter() 54 | alphaNumeric() 55 | } 56 | 57 | assertTrue(r in "a1b ab") 58 | } 59 | 60 | @Test fun anchors() { 61 | val r = regex { 62 | startOfString() 63 | literally("abc") 64 | endOfString() 65 | } 66 | 67 | assertTrue(r in "abc") 68 | assertTrue(r !in " abc") 69 | assertTrue(r !in "abc ") 70 | } 71 | 72 | @Test fun groups() { 73 | var groupId: Int = -1 74 | val r = regex { 75 | group("myGroup") { 76 | literally("abc") 77 | anyChar() 78 | } 79 | groupId = group { 80 | literally("def") 81 | digit() 82 | } 83 | matchGroup("myGroup") 84 | matchGroup(groupId) 85 | } 86 | 87 | val match = r.matchEntire("abcxdef2abcxdef2")!! 88 | assertEquals("def2", match.groupValues[groupId]) 89 | 90 | assertFalse(r in "abcxdef1abcydef2") 91 | } 92 | 93 | @Test fun any() { 94 | val r = regex { 95 | anyOf("abc", "def") 96 | anyOf({ literally("s") }, { 3 times { anyChar() } }) 97 | anyOf('1', '2', '3', '4') 98 | anyOf('a'..'z', 'A'..'Z') 99 | } 100 | 101 | assertTrue(r in "abcs1a") 102 | assertTrue(r in "defxxx3Z") 103 | } 104 | 105 | @Test fun included() { 106 | val r = regex { 107 | anyOf({ literally("inner") }, { anyChar() }) 108 | } 109 | val q = regex { 110 | literally("outerStart") 111 | include(r) 112 | literally("outerEnd") 113 | } 114 | 115 | assertTrue(q in "outerStart outerEnd") 116 | assertTrue(q in "outerStartinnerouterEnd") 117 | } 118 | 119 | @Test fun includeGroupIndices() { 120 | val inner = regex { 121 | group("someGroup") { 122 | literally("x") 123 | } 124 | } 125 | 126 | var g1 = -1 127 | val outerNoInclude = regex { 128 | group { literally("abc") } 129 | g1 = group { literally("def") } 130 | } 131 | 132 | var g2 = -1 133 | val outerInclude = regex { 134 | group { literally("abc") } 135 | include(inner) 136 | g2 = group { literally("def") } 137 | } 138 | 139 | val mNoInclude = outerNoInclude.matchEntire("abcdef") 140 | assertEquals("def", mNoInclude!!.groupValues[g1]) 141 | 142 | val mInclude = outerInclude.matchEntire("abcxdef") 143 | assertEquals("def", mInclude!!.groupValues[g2]) 144 | } 145 | } --------------------------------------------------------------------------------