Kotlin RegexRegex is generally refers to regular expression which is used to search string or replace on regex object. To use it functionality we need to use Regex(pattern: String) class. Kotlin'sRegex class is found in kotlin.text.regex package. Kotlin Regex Constructor
Regex Functions
Example of Regex class checking contains of input patternOutput: true The result of Regex function is based on matching regex pattern and the input string. Some function checks partial match while some checks full match. Regex example of containsMatchIn()Output: true Regex example of matches(input: CharSequence): BooleanThe matches(input: CharSequence): Booleanfunction of regex checksall input character sequence matches in regular expression. Output: false false true Regex example of matchEntire(input: CharSequence): MatchResult?The matchEntire() function is used to match complete input character from the pattern. Output: abcd null 100 null Regex example offind(input: CharSequence, startIndex: Int = 0): MatchResult?The find function is used to find the input character sequence from regex object. Output: [email protected] 123-456-7890 Regex example offindAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult>The findAll() function of regex returns sequence of match result on the basis of pattern provided. Output: 12 34 56 7 8 Regex example ofreplace(input: CharSequence, replacement: String): StringRegex replace() function replaces the all the matching pattern from input character sequence with specified replacement string. Output: this picture is awesome Regex example ofreplaceFirst(input: CharSequence, replacement: String): StringRegex replaceFirst() function replaces the first occurrence of matching pattern from input character sequence with specified replacement string. Output: nature is awesome, beautiful is nature Regex example ofsplit(input: CharSequence, limit: Int = 0): List<String>The regex split() function splits input character sequence according to pattern provided. This splits value are returned in List. Output: [ab, cd, ef] [nothing match to split] Next TopicKotlin Regex patterns |