Overview
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.
Pre-sets, Error Message & Validator
To utilise 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.
Next you'll need to add an appropriate error message for when the text does not match the Regex. If you have a Regex to match an email address to a particular domain then a custom error message such as 'email domian must be @softools.net' is friendlier and more meaningful to an end User who may not understand what the Regex expression means.
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.
Understanding how to build Regex Patterns
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]atTherefore, 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]atThis 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]. -
Capitalised 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.
-
\wmatches any letter, digit and underscore character -
\dmatches any digit that is the same as[0-9] -
\tmatches a tab character only -
\smatches 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 Specific Domains
Use Case
Match any email address from:
hotmail.comgmail.com
Regex
\b[A-Za-z0-9._%+-]{1,25}@(hotmail|gmail)\.com\bExplanation
\b
Matches a word boundary. This ensures the email address is not part of a larger word or string.
[A-Za-z0-9._%+-]
Matches valid username characters:
Letters (A–Z, a–z)
Numbers (0–9)
Period
.Underscore
_Percent
%Plus
+Hyphen
-
{1,25}
Requires between 1 and 25 characters before the @ symbol.
@
Matches the literal at-symbol.
(hotmail|gmail)
Matches either hotmail or gmail.
The pipe | functions as an OR operator.
\.com
Escapes the dot to match a literal period before com.
Final \b
Ensures the match ends cleanly at the domain and is not part of a longer string.
Match Word or Phrase from a Defined List
Use Case
Match any of the following:
Softools
Gridfields
Zero Code
Regex
(?i)\b(Softools|Gridfields|Zero\sCode)\bExplanation
(?i)
Enables case-insensitive matching.
This allows matches such as:
softoolsGRIDFIELDSZero Code
\b
Ensures each term is matched as a standalone word or phrase.
(Softools|Gridfields|Zero\sCode)
Matches any one of the listed values.
The pipe | acts as an OR operator.
\s
Matches a whitespace character between Zero and Code.
This allows for a space (or other whitespace) between the two words.
Final \b
Prevents matching inside longer words (e.g., MySoftoolsProject).
Comments
0 comments
Please sign in to leave a comment.