Rename methods in Regex. Add matchEntire method to match entire string against regex.

This commit is contained in:
Ilya Gorbunov
2015-10-14 07:44:19 +03:00
parent d860f335a3
commit d1d865aa0f
7 changed files with 80 additions and 26 deletions
+20 -2
View File
@@ -70,11 +70,29 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
* @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()`
* @return An instance of [MatchResult] if match was found or `null` otherwise.
*/
public fun match(input: CharSequence, startIndex: Int = 0): MatchResult? = nativePattern.findNext(input.toString(), startIndex)
public fun find(input: CharSequence, startIndex: Int = 0): MatchResult? = nativePattern.findNext(input.toString(), startIndex)
@Deprecated("Use find() instead.", ReplaceWith("find(input, startIndex)"))
public fun match(input: CharSequence, startIndex: Int = 0): MatchResult? = find(input, startIndex)
/** Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
*/
public fun matchAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = sequence({ match(input, startIndex) }, { match -> match.next() })
public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = sequence({ find(input, startIndex) }, { match -> match.next() })
@Deprecated("Use findAll() instead.", ReplaceWith("findAll(input, startIndex)"))
public fun matchAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = findAll(input, startIndex)
/**
* Attempts to match the entire [input] CharSequence against the pattern.
*
* @return An instance of [MatchResult] if the entire input matches or `null` otherwise.
*/
public fun matchEntire(input: CharSequence): MatchResult? {
if (pattern.startsWith('^') && pattern.endsWith('$'))
return find(input)
else
return Regex("^${pattern.trimStart('^').trimEnd('$')}$", options).find(input)
}
/**
* Replaces all occurrences of this regular expression in the specified [input] string with specified [replacement] expression.