What do you need help with?
Search the knowledge base, browse our resources, and visit our forum for more detailed information
Search the knowledge base, browse our resources, and visit our forum for more detailed information
Last updated: 3 May 2024
A regular expression, or regex, is a search pattern used for matching specific characters and ranges of characters within a string. It is widely used to validate, search, extract, and restrict text in most programming languages. KoboToolbox supports regex to control the length and characters during data entry to a particular question (e.g. controlling the entry of mobile number to exactly 10 digits, controlling the entry of a valid email id etc.).
Prepare a Text question type.
Go to the question’s Settings.
Go to Validation Criteria and choose the Manually enter your validation logic in XLSForm code option.
In the Validation Code box, enter your regex formula between the quotation
marks (' ')
of the regex(., ' ')
format. For reference, the period (.
)
refers to ‘this question’, while the regular expression inside the
quotation marks (' '
) needs to conform to the established regex rules.
(Optional) Add a custom Error Message for the person entering data to see when they don’t meet the regex criteria.
Regex can also be coded in XLSForm, under the constraint column:
type |
name |
label |
appearance |
constraint |
constraint_message |
---|---|---|---|---|---|
text |
q1 |
Mobile number of respondent |
numbers |
regex(., ‘^[0-9]{10}$’) |
This value must be only 10 digits |
Alternatively, you can create a calculate
question type and then define the
regex code under the calculation column. You could then use this variable as
many times as needed in the survey:
type |
name |
label |
calculation |
constraint |
constraint_message |
---|---|---|---|---|---|
calculate |
q0 |
‘^[A-Z]{1}[a-z]{1,}\s[A-Z]{1}[a-z]{1,}$’ |
|||
text |
q1 |
Name of the Enumerator |
regex(., ${q0}) |
Please use this format: Kobe Bryant |
|
text |
q2 |
Name of the Respondent |
regex(., ${q0}) |
Please use this format: Kobe Bryant |
|
integer |
q3 |
Age of the Respondent |
In addition to the examples and tips provided below, please visit this website for more help and examples.
Regex in KoboToolbox should always be written in-between the apostrophes regex(., ' ')
as shown in the examples.
Regex |
Description |
---|---|
|
The caret symbol matches the start of a string without consuming any character. |
|
The dollar symbol matches the end of a string without consuming any character. |
|
Matches either |
|
Matches any lowercase character from |
|
Matches any uppercase character from |
|
Matches any whole numbers from |
|
Matches any character from |
|
Matches any character except |
|
Matches any characters except those in the range |
|
The grouping character |
|
A vertical bar matches any element separated. |
|
A back slash is used to match the literal value of any metacharacter (e.g. try using |
|
Matches the same character as most recently matched by the nth (number used) capturing group. |
|
Matches any space or tab. |
|
Matches, without consuming any characters immediately between a character matched by |
|
Matches any equivalent numbers |
|
Matches anything other than numbers |
|
Matches any word character (i.e. |
|
Matches anything other than what |
|
A question mark used just behind a character matches or skips (if not required) a character match. |
|
An asterisk symbol used just behind a character matches zero or more consecutive character. |
|
The plus symbol used just behind a character matches one or more consecutive character. |
|
Matches exactly |
|
Matches at least |
|
Matches between |
Regex |
Description |
---|---|
|
Accepts lowercase and uppercase accents characters |
|
Accepts lowercase and uppercase accents characters but including letters with an umlaut (includes [ ] ^ \ × ÷) |
|
Accepts lowercase and uppercase accents characters but not including [ ] ^ \ |
|
Accepts lowercase and uppercase accents characters but not including [ ] ^ \ × ÷ |
For all text
type questions that use numbers, do not forget to type numbers
under the appearance
column.
Regex |
Description |
---|---|
|
Restrict mobile number to ten digits |
|
Restrict an input to |
|
Restrict an input between |
|
Restrict an input to either to |
|
Restrict an input of nine digits where the first number can’t be |
|
Restrict an input to one digit in between |
|
Restrict an input to five digits in between |
|
Restrict an input to two digits and three decimals (e.g. |
|
Restrict an input to two digits and three decimals (while the decimals are optional) (e.g. |
|
Restrict an input to |
Regex |
Description |
---|---|
|
Restrict an input to any lowercase letters (up to 6 characters long) |
|
Restrict an input to any uppercase letters (up to 10 characters long) |
|
Restrict an input to only either to |
|
Restrict an input to only |
|
Restrict an input of the beneficiaries name where the initials of the first name and last name are uppercase e.g. |
|
Restrict an input of the beneficiaries name with first name, middle name (if any) and last name e.g. |
|
Restrict an input of the beneficiaries’ full name where the initials of the names are in uppercase and the name are quite long (often greater than 3 words) e.g. |
|
Restrict an input of the beneficiaries’ first name (so that you are able to capture the exact spelling) where the enumerators are forced to enter the beneficiaries first name twice e.g. |
|
Restrict an input of the beneficiaries’ last name (so that you are able to capture the exact spelling) where the enumerators are forced to enter the beneficiaries last name twice e.g. |
|
Restrict a character within a word by using the |
|
Restrict a character within a word by using the |
|
Restrict a character within a word by using the |
|
Restrict an input to a non-digit character (e.g. |
|
Restrict an input to five non-digit character (e.g. |
|
Restrict all the text input to uppercase, excluding special characters (! @ # $ % ^ & * ( ) . , ? / ” ‘ etc.) |
|
Restrict all the text input to uppercase, including special characters (! @ # $ % ^ & * ( ) . , ? / ” ‘ etc.) |
|
Restrict all the text input to lowercase, excluding special characters (! @ # $ % ^ & * ( ) . , ? / ” ‘ etc.) |
|
Restrict all the text input to lowercase, including special characters (! @ # $ % ^ & * ( ) . , ? / ” ‘ etc.) |
XLSForm Regex |
Description |
---|---|
|
Restrict one character which matches between |
|
Restrict three character which matches between |
|
Restrict your beneficiary ID to a specific format e.g. |
|
Restrict your beneficiary ID to a specific format e.g. |
|
Restrict a currency input of three digits with a currency sign (either |
|
Restrict an exact input of number of words (e.g. to restrict exactly 3 words |
|
Restrict an input of number of words (e.g. to restrict a range of words say |
These examples are purely illustrative and should be adjusted for your use-case. Using regex for constraining email addresses does not guarantee that they are valid, only that they follow an expected pattern.
Regex |
Description |
---|---|
|
Restrict an input with a valid email address e.g. |
Regex |
Description |
---|---|
|
Restrict a time input in |
|
Restrict a time input in |
If you wish to use a regex constraint on a number in a text
type question,
make sure you always have the value numbers
under the appearance
column.
This restricts the display of alphabets, making only numbers visible for
inputs.
The Collect Android app and Enketo behave differently with their handling of
regex expressions. Collect behaves as if you have used the anchors ^
and $
around the expression (even if you have not used them), while Enketo requires
the anchors as mandatory for an exact match.
Did you find what you were looking for? Was the information clear? Was anything missing?
Share your feedback to help us improve this article!
KoboToolbox is maintained by Kobo Inc.