For instance, you could use a Regular Expression to ensure that an input is of a certain length, only contains certain characters, or matches a specific format.
Regular expressions can be very powerful and versatile tools. However, they can also be difficult to learn and use.
Simple Regular Expression Examples
Description
REGEX
Example That Matches
Example That Doesn't Match
Match any value
.
MIDAS
Value must start with the letters "BOOK"
^BOOK
BOOKED
BLUEBOOK
Value must end with the letters "ING"
ING$
BOOKING
BOOKED
Value must contain "BOOKING"
BOOKING
YOUR BOOKING DETAILS
YOU BOOKED
Value must be exactly "BOOKING"
^BOOKING$
BOOKING
YOUR BOOKING
Value must contain a number (digit)
\d
2 BOOKINGS
TWO BOOKINGS
Value must not contain a number (digit)
\D
TWO BOOKINGS
2 BOOKINGS
Value must be exactly "BOOK", followed by two single digit numbers
^BOOK\d\d$
BOOK43
BOOKING43
Value must start with "BOOK", followed by a three single digit number
^BOOK\d{3}
BOOK432
BOOK43
Value must contain "BOOK", followed by either a number 1, 2, or 3
BOOK[1-3]
BOOK2
BOOK4
Value must start with any uppercase letter
^[A-Z]
Abc123
abc123
Value must end with any lowercase letter
[a-z]$
123abc
abc123
Value must be at least 5 characters in length
.{5}
BOOKING
BOOK
Value must be exactly 4 characters in length
^.{4}$
BOOK
BOOKING
Match a number in the format ###-###-####
\d{3}-\d{3}-\d{4}
123-456-7890
123-4567-890
Explanation
Here's an explanation of some of the REGEX characters you'll have noticed in the above examples:
Expressions
Meaning
^
Matches at the start of a value
$
Matches at the end of a value
.
Matches any character
\d
Matches any single number (digit)
\D
Matches any single character that's not a number (digit)
{3}
Matches the preceding expression three times
[A-Z]
Matches any uppercase letter between A and Z
[a-z]
Matches any lowercase letter between A and Z
[A-Za-z]
Matches any letter between A and Z, regardless of case