Regular expressions

Regular expressions are used in Ceremony to validate a user input in a text field. It is very powerful to let you customize the validation.

You can find the full documentation on the syntax here

Ceremony and the script (see below) will validate a string only if the first match found by the regular expression equals the overall string.

Script to test your expression

To test your regular expression to ensure it validates the string, you can download the script regex_validation. It takes two arguments: the first one is the string to validate, and the second one is the regular expression to use. For example, to check that the string AZ3-D3F-45Y is validated by the regular expression [a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}", you can do:


    ./regex_validation 'AZ3-D3F-45Y' '[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}'
    

The script should output "String validated".

You may need to make the script executable with bash chmod +x and to accept to launch an unsigned script in your system preferences.

Command-line arguments

Note that as you can put specific characters in the string to validate or you can use specific characters in regular expression as operators, you should embrace the two strings with single quotation marks. You can use double quotation marks too, although this will cause problem when using the dollar sign $ because the command-line will replace it if don't use single quotation marks.

Special characters

To use the following characters in the regular expression (in command-line or in the configuration file), add an escape slash \ before: * ? + [ ( ) { } ^ $ | \ . /


Useful Examples

Regular expression

[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}

Validate a string which can contain only alphanumeric characters in 3 groups of 3 characters separated by hyphens.

The {n} operator allows to repeat n number of times the expression

Possible uses examples

Serial number, asset tag

Example of strings validated by the regular expression


Regular expression

MBP[1-9]{1,3}

Validate a string which begins by "MBP" and is followed by 1 or 3 digits.

The {n, m} operator allows to repeat n number of times the expression

Possible uses examples

When the MacBook Pro in your company are identified with MBP###

Example of strings validated by the regular expression

MBP23, MBP1, MBP458


Regular expression

[A-Z][a-z]*

Validate a string beginning by one capital letter, then any letter in the alphabet.

The * operator allows to repeat 0 or more times the expression.

Possible uses examples

First or last name.

Example of strings validated by the regular expression


Regular expression

(France|Germany).(Campus1|Campus2)

Validate a string which begins by "France" or "Germany" and is separated by a point to "Campus1" or "Campus2"

Possible uses examples

When the MacBook Pro in your company are identified with MBP###

Example of strings validated by the regular expression


Regular expression

\$[0-9]*

Validate a string which begins by a $ sign, followed by digits only.

Possible uses examples

Amount of money

Example of strings validated by the regular expression