Expressions

Regex expressions

11min

By combining regular (regex) expressions in your JSONata expressions in , you can create complex patterns to match specific strings of text. Regex expressions are a sequence of characters that define a search pattern. It is powerful when used in text processing to find, replace, or validate strings of text. Some common uses of regex include:

  • Validating email addresses or phone numbers
  • Extracting specific information from a text file or document
  • Reformatting data to match a specific pattern or structure

Examples and code snippets

The JSONata + regex examples below create validation for text-fields in a form.

Expected results: Between 9 and 13 numbers with no spaces, can include a symbol for dialing code, e.g., +271234556789.

Valid phone number
Valid phone number

Invalid phone number
Invalid phone number

YAML


Expected result: [email protected]

Valid email address
Valid email address

Invalid email address
Invalid email address

YAML


Expected result: Typically 13-16 digits, with spaces or dashes optional, and includes checks for Visa, MasterCard, American Express, and Discover. E.g. 1111-1111-1111-1111 or 1111 1111 1111 1111.

Valid  credit card number
Valid credit card number

Invalid  credit card number
Invalid credit card number

YAML


Expected result: 5-digit codes, e.g. 10036.

Valid ZIP code
Valid ZIP code

Invalid ZIP code
Invalid ZIP code

YAML


Expected result: XXX-XX-XXXX

Valid Social Security number
Valid Social Security number

Invalid Social Security number
Invalid Social Security number

YAML


Expected result: AA123456C

Valid National Insurance number
Valid National Insurance number

Invalid National Insurance number
Invalid National Insurance number

YAML


Expected result: DD/MM/YYYY, e.g. 23/07/2024.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Expected result: MM/DD/YYYY, e.g. 03/28/2023.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Expected result: DD Month YYYY, e.g. 25 July 2024.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Expected result: yyyy/mm/dd, e.g. 2024/08/30.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Expected result: 1234,00

Valid decimal number
Valid decimal number

Invalid decimal number
Invalid decimal number

YAML


Expected result: H:MM AM/PM e.g. 12:15 AM or 08:45 PM

Valid time format
Valid time format

Invalid time format
Invalid time format

YAML


Expected result: MM:SS / or HH:MM, e.g. 08:10.

Valid time format
Valid time format

Invalid time format
Invalid time format

YAML


Expected result: 01:00

Valid time format
Valid time format

Invalid time format

YAML


Expected result: example.com or http://example.com

Valid URL
Valid URL

Invalid URL
Invalid URL

YAML


Expected result: 978-1-4302-1998-9

Valid ISBN
Valid ISBN

Invalid ISBN
Invalid ISBN

YAML


Expected result: JohnSmith

Valid string -no spaces
Valid string -no spaces

Invalid string -no spaces
Invalid string -no spaces

YAML


Expected result: John Smith

Valid string with spaces
Valid string with spaces

Invalid string with spaces
Invalid string with spaces

YAML


Expected result: 56575 76 6

Valid numbers with spaces
Valid numbers with spaces

Invalid numbers with spaces
Invalid numbers with spaces

YAML