The Regex (Regular Expression) pattern empowers us to integrate validation into your text field, providing control over the data input. This validation feature serves as a mechanism to limit the characters entered into your text box. For example, we can set parameters that restrict the user to input only characters from A to H, or confine the input exclusively to lowercase characters, among other possible constraints.
Presets and Validator
To utilize the pattern feature, start by creating a text field. You'll find a dropdown menu under the option Pattern offering a selection of popular pre-set regex patterns to choose from. If these presets don't align with your requirements, you have the option to input your own custom regex. Highlighted below there's a button that opens additional options for pattern modification, granting you further flexibility to tailor the pattern as per your needs.
You'll then be directed to a screen that provides the same number of preset options as before, but this time, the regex box gets populated automatically. You can either edit this pre-filled regex pattern with extra validation rules or forgo the preset and craft a regex expression from scratch. To ensure the efficacy of the validation without publishing, a 'Test' option is available. This feature allows you to verify whether the input you've entered aligns with the defined validation rules.
For this advanced guide, its important to establish the terminologies that will be used:
- pattern: regular expression pattern
- string: a sequence of characters used to match the pattern
- digit: 0-9
- letters: between a-z & A-Z
- symbol: !$%^&*()_+|~-=`{}[]:”;'<>?,./
- space: single white space, tab
- character: refers to a letter, digit or symbol
Character Sets
If we want to match the terms "bat," "cat," and "fat." This can be accomplished by utilising character sets, signified by the symbol []. Essentially, you enter several characters to be matched.
So, the regex for this use case would be:
[bcf]at
Therefore, if an input did not contain any string that matched the Regex a validation error will be displayed.
Ranges
Taking the previous example, if we wanted to further expand this and match all words that end in "at".
We can implement Ranges. See below:
[a-z]at
This Regex pattern now will match all words that end with at.
Here are some more ways in which you can use ranges:
-
Partial range: selections such as
[a-f]
or[g-p]
. - Capitalized range:
[A-Z]
-
Digit range:
[0-9]
. -
Symbol range: for example,
[£$#%@&]
Repeating Characters
If you wanted to match a 4 letter word we can do the following:
[a-z][a-z][a-z][a-z]
This would match all words with four letters. However, if we wanted to match an eight - or nine-character word? The procedure outlined above is arduous. There is a more concise method to represent this pattern with the curly braces notation. All that is required is that you provide the quantity of repeating characters.
b{5}
will match “bbbbb”.e{3}
will match “eee”.[a-z]{4}
will match any four-letter word such as “room”, “cook” or “word”.[a-z]{7,}
will match any word with seven or more letters.[a-z]{4,9}
will match any word between four and nine letters.[0-9]{11}
will match an 11-digit number. This method can be used to validate basic international phone numbers.
Metacharacters
Metacharacters enable you to build even more concise regular expression patterns.
\w
matches any letter, digit and underscore character\d
matches any digit that is the same as[0-9]
\t
matches a tab character only\s
matches a whitespace character — that is, a space or tab
So for example:
The Regex: \d{11}
will match an 11-digit number such as a phone number
Special Characters
Special characters enable us to write more complex pattern expressions:
+
: One or more quantifiers: The preceding character must be present and may be duplicated
So for example the Regex: do+or
will match 'door' or 'doooooooor'
?
: Zero or one quantifier: This means that the preceding character is optional
So, for example the Regex: w?hat
will match 'what' or 'hat'
*
: Zero or more quantifier: This is essentially + and ? combined. So, the preceding character is optional and may be duplicated
[^]
: This "negate" sign indicates that a character should not be matched within a range.
Groups
All of the special characters listed above have an effect on a single character or a range of characters. If we wanted to apply the effect on a subset of the expression then we can utilise round brackets — ()
For example, the pattern softools(.net)?
will match both “softools” and “softools.net”, since we’ve made the “.net” part optional.
Alternating Characters
Using the "pipe" symbol — |, we can provide alternate characters. So, for example the pattern Fields|Gridfields
will match both “Fields” and “Gridfields” strings
Start and End of Pattern
If there was a pattern to match the string "soft," the string "softools" will also receive a positive match, despite the fact that the match is not exactly "soft. This is where the following notations come into use:
^ and $
^
: inserted at the beginning, this character matches a pattern at the beginning of a string.$
: inserted at the end, this character matches a pattern at the end of the string.
So, the Regex: ^soft$
. This will strictly match the word “soft”. When both are used, both rules are followed.
Use Case Examples
Match Any Email Address from a Specific Domain | |
Usage Example | Match any email address from the domains hotmail.com, and gmail.com. |
Regex | (\W|^)[\w.\-]{0,25}@(hotmail|gmail)\.com(\W|$) |
Notes |
|
Match Word or Phrase |
|
Usage Example | Match any word or phrase in the following list:
|
Regex | (?i)(\W|^)(Softools|Gridfields|Zero\sCode)(\W|$) |
Notes |
|
Comments
0 comments
Please sign in to leave a comment.