Regex


What is Kotlin Regex?

Kotlin Regular Expressions are used for text search and more advanced text manipulation. Text editors, such as grep, sed, vi, and Emacs, have built-in expressive tools for tools such as programming languages, including Kotlin, JavaScript, Perl, and Python.

Kotlin Regex refers to a regular expression used to search for a string or replace it with a Regex object. We need to use the Regex (pattern: string) class to utilize this functionality.

A pattern is a general expression that defines the text we are looking for or manipulating. It contains text literals and metacharacters. Metacharacters are special characters that control how regular expression is evaluated. For example, w \ s is used to search for white spaces, and special characters must be double-escaped, or Kotlin Raw strings are to be used.

Once we have created a pattern, we can use one of the functions to apply the way to a text string. Functions include matches, contains matchIn, find, findreplace, and split.

Some of the most commonly used expressions are shown below:

Regex Meaning
. listOf()
? mapOf()
+setOf()
* listOf()
^ mapOf()
$setOf()
| listOf()
[abc] mapOf()
[a-c]setOf()
[^abc] listOf()
\s mapOf()
\wsetOf()

How do I add Regex to Kotlin?

  • Constructor- (pattern:string): This constructor creates a standard expression based on the pattern string.
  • n(pattern:String, Option:RegexOption): This constructor creates a standard expression based on a specific pattern and option. This option is a constant of the RegexOption enum class.

What does the Kotlin Regex do?

A regular expression (abbreviated as Regex or regexp; also known as a Rational Expression) is an array of characters that specify a search pattern in the text. Typically such practices use string-search algorithms to "find" or "find and replace" or input validation for actions on strings.

The matches and containsMatchIn Methods in Kotlin

The matching method is true if the regular expression matches the entire input string. The containsMatchIn method specifies whether a regular expression can detect at least one match in a given input.

Find Method – The find method provides the first match of a regular expression on the input, starting with the specified start index.

findAll Method – The findAll Method provides a sequence of all the events of a regular expression within the input string.

Regex word boundaries – A metacharacter \b is an anchor that fits into a position called the word border. This allows searching for the entire words.

Regex implicit word boundaries – \w is a character class used for a permissible character in a word. For regular expressions referring to a word \w +, the boundary metacharacters of the word at the front and back are unclear; that is, \w + is equal to \b \w + \b.

Currency Symbols - Regular expression \ p {Sc} can be used for currency symbols.

Regex Anchors

The anchors match the positions of the characters within the given text. Using this ^ anchor, the match must occur at the beginning of the string when using the anchor and the match at the end of the string when using the $ anchor.

Regex Alternations - Alternation Operator | Creates a common expression with many choices.

Regex subpatterns - Subpatterns are patterns within patterns. Sub-patterns are created using () characters.

Regex Character Classes - A character class defines a set of characters that can occur in the input string to win a match.

Regex Capturing Groups - () is used to create capturing groups. This allows us to apply a quantifier to the whole group or to limit it to one part of the regular expression alternately.