Skip to main content

Mask Type: Simplified Regular Expressions

  • 4 minutes to read

Simplified regular expressions allow you to specify the entered text’s pattern.

image

Run the following Demo Center module to test various mask types: Mask Box.

Tip

Simplified regular expressions support backward compatibility with XtraEditors version 2. We recommend that you use the RegEx type syntax because it supports additional features like alternative validation and autocomplete.

Metacharacters

Metacharacters specify characters users can enter at the corresponding positions. The table below lists the available metacharacters.

Character Description
. Any single character.
[aeiou] Any single character from the specified character set.
[^aeiou] Any single character not from the specified character set.
[0-9a-fA-F] Any single character from the specified character range.
\w Any single alphanumeric character. The same as [a-zA-Z_0-9].
\W Any single non-alphanumeric character. The same as [^a-zA-Z_0-9].
\d Any single numeric character. The same as [0-9].
\D Any single non-numeric character. The same as [^0-9].

Quantifiers

Quantifiers follow a metacharacter and specify how many times the character should be repeated. The table below lists the available qualifiers.

Quantifier Description Examples
* Zero or more matches. The same as {0,}. [a-zA-Z]*, \w*
+ One or more matches. The same as {1,}. [a-zA-Z]+, \w+
? Zero or one matches. The same as {0,1}. [a-zA-Z]?, \w?
{n} Exactly n matches. [0-9]{2}
{n,} At least n matches. [0-9]{2,}
{n,m} At least n, but not more than m matches. [0-9]{2,7}

Special Characters

Special characters convert the entered text to uppercase/lowercase, insert delimiters and currency symbols. The table below lists the available special characters.

Character Description
> Converts subsequent characters to uppercase.
< Converts subsequent characters to lowercase.
<> Discards case conversion for subsequent characters.
/ Separates months, days, and years. The culture’s DateSeparator property specifies the actual character.
: Separates hours, minutes, and seconds. The culture’s TimeSeparator property specifies the actual character.
$ Inserts the currency symbol. The culture’s CurrencySymbol property specifies the actual character.

Literal Characters

Literal characters are arbitrary read-only characters in the edit box. If you add a character that is not a meta or special character into a mask expression, the character is displayed in the edit box as is. To display a meta or special character as a literal, precede it with a backslash.

Examples

Example 1

[0-9A-F]* - a mask to enter hexadecimal values of any length.

CD_Mask_Regular_hex

Example 2

[0-9A-F]{2,} - a mask to enter a hexadecimal value that consists of at least two digits.

If the user enters less than two digits, the editor displays a notification.

CD_Mask_Regular_hex_invalid

Example 3

\d*\.\d{2} - a mask to enter a number that has two decimal digits.

CD_Mask_Regular_number2

Regular Expression Settings

When you click the ellipsis button next to the MaskSettings property, the Mask Settings dialog appears. Click “Show Advanced Options” in the bottom left corner to view optional Regular Expression settings.

Simple Regular Settings

“Placeholder character”

Allows you to choose a character displayed for empty metacharacters. The default placeholder character is underscore (“_”).

//Fluent API
using DevExpress.XtraEditors.Mask;

textEdit5.Properties.MaskSettings.Configure<MaskSettings.Regular>(settings => {
    settings.MaskExpression = "[0-9A-Z]{10}";
    settings.Placeholder = 'X';
});

//regular API
using DevExpress.XtraEditors.Mask;

var settings = textEdit5.Properties.MaskSettings.Configure<MaskSettings.Regular>();
settings.MaskExpression = "[0-9A-Z]{10}";
settings.Placeholder = 'X';

//Data Annotation Attribute (for code-first sources)
using System.ComponentModel.DataAnnotations;

[RegularEditMask("[0-9A-Z]{10}", Placeholder = 'X')]
public string ID { get; set; }

“Save static literals to edit value”

This property specifies whether read-only literal characters should be saved to the editor’s EditValue. For example, if the editor mask is “ID: [0-9A-Z]{10}”, the editor saves only ten digits as its EditValue if this property is disabled, and the entire “ID: 1234567890” string if enabled.

//Fluent API
using DevExpress.XtraEditors.Mask;

textEdit5.Properties.MaskSettings.Configure<MaskSettings.Regular>(settings => {
    settings.MaskExpression = "[0-9A-Z]{10}";
    settings.SaveLiterals = true
});

//regular API
using DevExpress.XtraEditors.Mask;

var settings = textEdit5.Properties.MaskSettings.Configure<MaskSettings.Regular>();
settings.MaskExpression = "[0-9A-Z]{10}";
settings.SaveLiterals = true

//Data Annotation Attribute (for code-first sources)
using System.ComponentModel.DataAnnotations;

[RegularEditMask("[0-9A-Z]{10}", SaveLiterals = true)]
public string ID { get; set; }
See Also