Mask Type: Extended Regular Expressions
- 6 minutes to read
Extended regular expressions allow you to specify a pattern that the entered text should match. The syntax is similar to the syntax defined in the POSIX ERE specification.
Note
Run the XtraEditors demo to try out input masks.
Tip
To allow users to enter numeric or date-time values you can also use the numeric and date-time mask types.
Mask Type and Mask Expression
The TextEdit.Properties.Mask property provides access to a MaskProperties class instance that specifies an input mask. Set the MaskProperties.MaskType property to RegEx to enable the regex mask format. Use the MaskProperties.EditMask property to specify the mask expression.
Metacharacters
Metacharacters specify characters users can enter at the corresponding positions. The table below lists the available metacharacters.
Character | Description |
---|---|
| Any character. |
| Any single character from the specified character set. |
| Any single character not from the specified character set. |
| Any single character from the specified character range. |
| Any single alphanumeric character. The same as |
| Any single non-alphanumeric character. The same as |
| Any single numeric character. The same as |
| Any single non-numeric character. The same as |
| Any single whitespace (space, tab, etc.). |
| Any single non-whitespace character. |
| An ASCII character. For example, |
| A Unicode character. For example, |
| Any character from the specified Unicode character category. For example, Letter (L) - any letter. UppercaseLetter (Lu) - an uppercase letter. Entered characters are converted to uppercase. LowercaseLetter (Ll) - a lowercase letter. Entered characters are converted to lowercase. Number (N) - any number. Symbol (S) - a mathematical symbol, currency symbol, or a modifier symbol. Punctuation (P) - any punctuation mark. Separator (Z) - any separator. See UnicodeCategory for information on other categories. |
| Any character not from the specified Unicode character category. |
| A character that separates a number’s integer and fractional parts. The culture’s NumberDecimalSeparator property specifies the actual character. |
| A character that separates hours, minutes, and seconds. The culture’s TimeSeparator property specifies the actual character. |
| A character that separates months, days, and years. The culture’s DateSeparator property specifies the actual character. |
| The specified character. For example, DayNames - a culture-specific full weekday name. The culture’s DayNames property specifies day names. MonthNames - a culture-specific full month name. The culture’s MonthNames property specifies month names. AbbreviatedDayNames - a culture-specific abbreviated weekday name. The culture’s AbbreviatedDayNames property specifies abbreviated day names. AbbreviatedMonthNames - a culture-specific abbreviated month name. The culture’s AbbreviatedMonthNames property specifies abbreviated month names. AMDesignator - the string designator for hours that are “ante meridian” (before noon). The culture’s AMDesignator property specifies the actual character. PMDesignator - the string designator for hours that are “post meridian” (after noon). The culture’s PMDesignator property specifies the actual character. DateSeparator - the date separator. The same as TimeSeparator - the time separator. The same as NumberDecimalSeparator - the decimal separator in numeric values. The same as CurrencyDecimalSeparator - the decimal separator in currency values. The culture’s CurrencyDecimalSeparator property specifies the actual character. CurrencySymbol - the currency symbol. The culture’s CurrencySymbol property specifies the actual character. NumberPattern - any numeric value in the culture’s number format. The culture’s NumberFormat property provides access to number pattern settings. CurrencyPattern - any currency value in the culture’s currency format (without the currency symbol). The culture’s NumberFormat property provides access to currency pattern settings. |
Any other character. | Any other character that is not a metacharacter, quantifier, or special character specifies itself. For example, To allow users to enter a special character, precede it with a backslash. For example, |
Quantifiers
Quantifiers follow a metacharacter and specify how many times the character should be repeated. The table below lists available qualifiers.
Quantifier | Description | Samples |
---|---|---|
* |
Zero or more matches. | \w* - zero or more letters. The same as \w{0,} . |
+ |
One or more matches. | \w+ - one or more letters. The same as \w{1,} . |
? |
Zero or one matches. | \w? - zero or one letter. The same as \w{0,1} . |
{n} |
Exactly n matches. | \d{4} - four digits. |
{n,} |
At least n matches. | \d{2,} - two or more digits. |
{n,m} |
At least n, but no more than m matches. | \d{1,3} - one, two, or three digits. |
Special characters
Special characters allow you to group expression parts and provide alternatives. The table below lists the available special characters.
Character | Description | Samples |
---|---|---|
| Alternate several expressions. |
|
| Groups several characters into a single unit. |
|
Precedence
The order of precedence is as follows:
- Escaped Characters:
\
. Sets:[]
. - Grouping:
()
. - Quantifiers:
*
,+
,?
,{}
. - Concatenation.
- Alternation:
|
.
Examples
Example 1
\d+(\R.\d{0,2})?
- numeric values with or without the fractional part.
\d+
stands for any number of decimal digits. \R.
- the decimal point. \d{0,2}
- 0, 1 or 2 digits. ( )?
- the inner expression appears only once.
Example 2
(1?[1-9])|([12][0-4])
- numbers in the range of 1-24.
|
alternates (1?[1-9])
and ([12][0-4])
. The first part matches numbers in the ranges of 1-9 and 11-19. The second part matches numbers in the ranges of 10-14 and 20-24.