# Using regular expressions in XLSForm **Last updated:** 21 Mar 2026 A **regular expression**, or regex, is a search pattern used to match specific characters or character ranges within text. Regular expressions are commonly used to validate, search, extract, or restrict text input. In KoboToolbox, regular expressions are typically used to control how users enter data. For example, you can restrict a mobile number to exactly 10 digits, enforce a specific ID format, or limit text to uppercase letters only. This article provides an overview of common regular expression components and practical examples you can use to validate and restrict text input in your forms.

To learn more about using regular expressions in your forms, see Introduction to form logic in XLSForm and Introduction to form logic in the Formbuilder.

## Common regex components Regular expressions in KoboToolbox are written inside the `regex()` function. For example, `regex(., '^[0-9]{10}$')` restricts an input to exactly 10 digits. The following tables describe commonly used regex elements. ### Anchors and grouping | Regex | Description | |:---|:---| | ^ | Matches the start of a string | | $ | Matches the end of a string | | ( ) | Groups characters together | | \| | Matches one pattern or another (logical OR) | ### Character classes | Regex | Description | |:---|:---| | [abc] | Matches a, b, or c | | [a-z] | Matches any lowercase letter | | [A-Z] | Matches any uppercase letter | | [0-9] | Matches any digit from 0 to 9 | | [a-zA-Z0-9] | Matches letters or digits | | \[^abc] | Matches any character except a, b, or c | | \[^A-Z] | Matches any character except uppercase letters | ### Special character shortcuts | Regex | Description | |:---|:---| | \d | Matches any digit (same as [0-9]) | | \D | Matches any non-digit | | \w | Matches any word character (letters, digits, _) | | \W | Matches any non-word character | | \s | Matches a space or tab | | \b | Matches a word boundary | | \\. | Matches a literal dot (.) | | \\@ | Matches a literal @ | | \\$ | Matches a literal $ | | `\\` | Matches a literal backslash (\\) | | \number | Refers to a previously matched group | ### Quantifiers | Regex | Description | |:---|:---| | ? | Matches zero or one occurrence | | * | Matches zero or more occurrences | | + | Matches one or more occurrences | | {x} | Matches exactly x occurrences | | {x,} | Matches at least x occurrences | | {x,y} | Matches between x and y occurrences | ## Common examples The following examples can be used as [constraints](https://support.kobotoolbox.org/constraints_xls.html) or [validation criteria](https://support.kobotoolbox.org/validation_criteria.html) in your forms. ### Common regex expressions with numbers | Regex | Description | |:---|:---| | regex(., '^\d+$') | Limit input to numbers | | regex(., '^\d{2}$') | Limit input to 2 digits | | regex(., '^\d{2,4}$') | Limit input to 2-4 digits | | regex(., '^\d{10}$') | Limit input to 10 digits | | regex(., '^[1-9][0-9]?$|^100$') | Limit input to a number between 1 and 100 (e.g., for a percentage) | | regex(., '^[1-9][0-9]{8}$') | Limit input to 9 digits, first digit cannot be 0 | | regex(., '^\d{2}\\.\d{3}$') | Limit input to number formatted as 12.345 | | regex(., '^(\d{10}|\d{13}|\d{17})$') | Limit input to 10, 13, or 17 digits | | regex(., '^(12|345)$') | Input must be 12 or 345 | ### Common regex expressions with letters | Regex | Description | |:---|:---| | regex(., '^\D*$') | Limit input to letters, spaces, and symbols (no numbers) | | regex(., '^[A-Za-z]+$') | Limit input to letters | | regex(., '^[A-Za-z\s]+$') | Limit input to letters and spaces | | regex(., '^[a-z]{1,6}$') | Limit input to 1–6 lowercase letters | | regex(., '^[A-Z]{1,10}$') | Limit input to 1–10 uppercase letters | | regex(., '^(Apple|Orange|Banana)$') | Input must match one word from the list | | regex(., '^colou?r$') | Allows color or colour | | regex(., '^[A-Z\s]+$') | Limit input to uppercase letters and spaces only | | regex(., '^[a-z_]+$') | Limit input to lowercase letters and underscores (_) | ### Common regex expressions with letters and numbers | Regex | Description | |:---|:---| | regex(., '^\w{3}$') | Limit input to exactly 3 letters, digits, or underscores (_) | | regex(., '^[A-Z]{1}[0-9]{8}$') | Limit input to one letter and eight numbers | | regex(., '^CAR-PRC-2020-\d{4}$') | Limit input to a specific ID format | | regex(., '^\W*(\w+\b\W*){3}$') | Limit input to exactly 3 words | | regex(., '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*(\\\.[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)*\\\.[a-zA-Z]{2,}$') | Limit input to common email format |

For additional help building and testing patterns, visit: http://www.regexr.com/