Rename Regex.hasMatch to containsMatchIn, and add corresponding contains function to CharSequence.
This commit is contained in:
@@ -59,12 +59,15 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
|
|||||||
return match != null && (match as RegExpMatch).index == 0 && nativePattern.lastIndex == input.length()
|
return match != null && (match as RegExpMatch).index == 0 && nativePattern.lastIndex == input.length()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Indicates whether the regular expression can find at least a match in the specified [input]. */
|
/** Indicates whether the regular expression can find at least one match in the specified [input]. */
|
||||||
public fun hasMatch(input: CharSequence): Boolean {
|
public fun containsMatchIn(input: CharSequence): Boolean {
|
||||||
nativePattern.reset()
|
nativePattern.reset()
|
||||||
return nativePattern.test(input.toString())
|
return nativePattern.test(input.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("Use containsMatchIn() or 'in' operator instead.", ReplaceWith("this in input"))
|
||||||
|
public fun hasMatch(input: CharSequence): Boolean = containsMatchIn(input)
|
||||||
|
|
||||||
/** Returns the first match of a regular expression in the [input], beginning at the specified [startIndex].
|
/** Returns the first match of a regular expression in the [input], beginning at the specified [startIndex].
|
||||||
*
|
*
|
||||||
* @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()`
|
* @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()`
|
||||||
|
|||||||
@@ -777,6 +777,12 @@ public operator fun String.contains(seq: CharSequence, ignoreCase: Boolean = fal
|
|||||||
public operator fun String.contains(char: Char, ignoreCase: Boolean = false): Boolean =
|
public operator fun String.contains(char: Char, ignoreCase: Boolean = false): Boolean =
|
||||||
indexOf(char, ignoreCase = ignoreCase) >= 0
|
indexOf(char, ignoreCase = ignoreCase) >= 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if this string contains at least one match of the specified regular expression [regex].
|
||||||
|
*/
|
||||||
|
public operator fun CharSequence.contains(regex: Regex): Boolean = regex.containsMatchIn(this)
|
||||||
|
|
||||||
|
|
||||||
// rangesDelimitedBy
|
// rangesDelimitedBy
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -114,8 +114,11 @@ public class Regex internal constructor(private val nativePattern: Pattern) {
|
|||||||
/** Indicates whether the regular expression matches the entire [input]. */
|
/** Indicates whether the regular expression matches the entire [input]. */
|
||||||
public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches()
|
public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches()
|
||||||
|
|
||||||
/** Indicates whether the regular expression can find at least a match in the specified [input]. */
|
/** Indicates whether the regular expression can find at least one match in the specified [input]. */
|
||||||
public fun hasMatch(input: CharSequence): Boolean = nativePattern.matcher(input).find()
|
public fun containsMatchIn(input: CharSequence): Boolean = nativePattern.matcher(input).find()
|
||||||
|
|
||||||
|
@Deprecated("Use containsMatchIn() or 'in' operator instead.", ReplaceWith("this in input"))
|
||||||
|
public fun hasMatch(input: CharSequence): Boolean = containsMatchIn(input)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the first match of a regular expression in the [input], beginning at the specified [startIndex].
|
* Returns the first match of a regular expression in the [input], beginning at the specified [startIndex].
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class RegexTest {
|
|||||||
assertFalse(input.matches(p))
|
assertFalse(input.matches(p))
|
||||||
assertFalse(p.matches(input))
|
assertFalse(p.matches(input))
|
||||||
|
|
||||||
assertTrue(p.hasMatch(input))
|
assertTrue(p in input)
|
||||||
|
|
||||||
val first = p.find(input)
|
val first = p.find(input)
|
||||||
assertTrue(first != null); first!!
|
assertTrue(first != null); first!!
|
||||||
|
|||||||
Reference in New Issue
Block a user