Regular expressions are an advanced tool that helps you validate user input, for example to validate Member IDs or Customer Numbers when validating your forms.
Let’s say your customers are to fill a form where one of the fields is a Customer ID, composed of three lowercase letters, followed by a dash, and then three to five digits, so abc-123 or xyz-12345.
To avoid the usual invalid variations such as ABC-123 or Abc-123 or abc123 or abc-123456, Regular Expressions come to the rescue! To give you an idea, here’s the regular expression that would validate the aforementioned format, and decline all the bad format examples:
^[a-z]{3}-[0-9]{3,5}$
Confused? Worry not, you can find all the tools to help you below.
Note: there is a lot more you can do with Regular Expressions than what is listed in this document, but this should give you the basic knowledge to cover almost every case.
From A To Z With Regular Expressions
The best way to learn is to divide and conquer, so let’s take it step by step and start with the single letter or the single digit.
a
– will identify only and exclusively the letter “a”, so far so good
0
– will identify only and exclusively the digit 0, you would have guessed that
[a-z]
– will identify any single lowercase letter
because [ ] means “a character” and the a-z means “from a to z”, so also
[A-Z]
– will identify any single uppercase letter, and believe it or not…
[a-zA-Z]
– will identify any single letter! From A to Z, lowercase or uppercase
and then digits
[0-9]
– will identify any single digit, and so
[a-z0-9]
– will identify any single lowercase letter or digit
and well there is also
.
– which will identify… any character which can be confusing, so just ignore it for now
Sequences
Now that you know how to identify single letters and digits its important you know how to sequence them.
a{3}
– will match only and exclusively 3 occurrences in sequence of “a”, so “aaa”
a{3,5}
– will match only and exclusively 3 to 5 occurrences in sequence of “a”, and so
[a-z]{3}
– will match 3 occurrences in sequence of any lowercase letter
[a-zA-Z]{1,3}
– will match 1 to 3 occurrences in sequence of any letter
and then you can also sequence different character rules, so that
[a-z][A-Z]
– will match one lowercase letter, followed by an uppercase letter
and for the big finale about sequences
[a-z]*
– will match 0 or more lowercase letters
[a-z]+
– will match 1 or more lowercase letters
Getting A Full Match
More often than not you will want to write a Regular Expression that covers and validates the entirety of the user’s input and for that you add ^ at the start and $ at the end, so that:
abc
– will also match “whatever has abc included”
[a-z]{3}
– will also match “the same as above”
but if you limit it…
^abc$
– then it will match only and exclusively “abc”
^[a-z]{3}$
– then it will match only and exclusively three lowercase letters
Special Characters
Bare in mind that the following characters are special characters and so if you wish to use them you are advised to put a \ behind them… just in case.
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
So for example if you want to match any sequence of letters and dots, such as:
some.words.with.dots.in.between
You can do that by using:
[\.a-z]+
– which will match 1 or more lowercase letters and dots, in any order
Need Help?
Worry not! Just reach out using our LiveChat support and we’ll be happy to help and provide you with a fancy Regular Expression for your input field validation.