81
Kotlin Regex Pattern
Regex uses several symbolic notation (patterns) in its function. Some commonly uses patterns are given below:
Symbol | Description |
---|---|
x|y | Matches either x or y |
xy | Matches x followed by y |
[xyz] | Matches either x,y,z |
[x-z] | Matches any character from x to z |
[^x-z] | ‘^’ as first character negates the pattern. This matches anything outside the range x-z |
^xyz | Matches expression xyz at beginning of line |
xyz$ | Matches expression xyz at end of line |
. | Matches any single character |
Regex Meta Symbols
Symbol | Description |
---|---|
d | Matches digits ([0-9]) |
D | Matches non-digits |
w | Matches word characters |
W | Matches non-word characters |
s | Matches whitespaces [trfn] |
S | Matches non-whitespaces |
b | Matches word boundary when outside of a bracket. Matches backslash when placed in a bracket |
B | Matches non-word boundary |
A | Matches beginning of string |
Z | Matches end of String |
Regex Quantifiers Patterns
Symbol | Description |
---|---|
abcd? | Matches 0 or 1 occurrence of expression abcd |
abcd* | Matches 0 or more occurrences of expression abcd |
abcd+ | Matches 1 or more occurrences of expression abcd |
abcd{x} | Matches exact x occurrences of expression abcd |
abcd{x,} | Matches x or more occurrences of expression abcd |
abcd{x,y} | Matches x to y occurrences of expression abcd |
Regex Sample Patterns
Pattern | Description |
---|---|
([^s]+(?=.(jpg|gif|png)).2) | Matches jpg,gif or png images. |
([A-Za-z0-9-]+) | Matches latter, number and hyphens. |
(^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^100$) | Matches any number from 1 to 100 inclusive. |
(#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) | Matches valid hexa decimal color code. |
((?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) | Matches 8 to 15 character string with at least one upper case, one lower case and one digit. |
([email protected][a-zA-Z_]+?.[a-zA-Z]{2,6}) | Matches email address. |
(<(/?[^>]+)>) | Matches HTML tags. |
Next TopicKotlin