diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index b5a70244529..fe398a4a0c3 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -153,10 +153,13 @@ public class Regex(pattern: String, options: Set) { } } -/** - * Creates a regular expression from the specified [pattern] string and the specified [options]. - */ -public fun Regex(pattern: String, vararg options: RegexOption): Regex = Regex(pattern, options.toSet()) +/** Creates a regular expression from the specified [pattern] string and the specified single [option]. */ +public fun Regex(pattern: String, option: RegexOption): Regex = Regex(pattern, setOf(option)) + +/** Creates a regular expression from the specified [pattern] string and the default options. */ +public fun Regex(pattern: String): Regex = Regex(pattern, emptySet()) + + private fun RegExp.findNext(input: String, from: Int): MatchResult? { diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt index e5cb6f8ff51..344543b96b0 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt @@ -1,10 +1,16 @@ package kotlin import kotlin.text.* + /** - * Converts the string into a regular expression [Regex] with the specified [options]. + * Converts the string into a regular expression [Regex] with the default options. */ -public fun String.toRegex(vararg options: RegexOption): Regex = Regex(this, *options) +public fun String.toRegex(): Regex = Regex(this) + +/** + * Converts the string into a regular expression [Regex] with the specified single [option]. + */ +public fun String.toRegex(option: RegexOption): Regex = Regex(this, option) /** * Converts the string into a regular expression [Regex] with the specified set of [options]. diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt index c305963d7c3..81ceebeacec 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt @@ -90,11 +90,16 @@ public data class MatchGroup(public val value: String, public val range: IntRang */ public class Regex internal (private val nativePattern: Pattern) { + + /** Creates a regular expression from the specified [pattern] string and the default options. */ + public constructor(pattern: String): this(Pattern.compile(pattern)) + + /** Creates a regular expression from the specified [pattern] string and the specified single [option]. */ + public constructor(pattern: String, option: RegexOption): this(Pattern.compile(pattern, ensureUnicodeCase(option.value))) + /** Creates a regular expression from the specified [pattern] string and the specified set of [options]. */ public constructor(pattern: String, options: Set): this(Pattern.compile(pattern, ensureUnicodeCase(options.toInt()))) - /** Creates a regular expression from the specified [pattern] string and the specified [options]. */ - public constructor(pattern: String, vararg options: RegexOption) : this(pattern, options.toSet()) /** The pattern string of this regular expression. */ public val pattern: String @@ -189,7 +194,7 @@ public class Regex internal (private val nativePattern: Pattern) { companion object { /** Returns a literal regex for the specified [literal] string. */ - public fun fromLiteral(literal: String): Regex = Regex(literal, RegexOption.LITERAL) + public fun fromLiteral(literal: String): Regex = literal.toRegex(RegexOption.LITERAL) /** Returns a literal pattern for the specified [literal] string. */ public fun escape(literal: String): String = Pattern.quote(literal) /** Returns a literal replacement expression for the specified [literal] string. */ diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index 9047ef90c57..8259de889b3 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -87,7 +87,7 @@ class RegexTest { test fun escapeLiteral() { val literal = """[-\/\\^$*+?.()|[\]{}]""" assertTrue(Regex.fromLiteral(literal).matches(literal)) - assertTrue(Regex(Regex.escape(literal)).matches(literal)) + assertTrue(Regex.escape(literal).toRegex().matches(literal)) } test fun replace() {