links
B | There are 3 types of brackets used in regular expression Square brackets “[“and Curly “{“ brackets. Square brackets specify the character which needs to be matched while curly brackets specify how many characters. “(“ for grouping. We will understand the same as we move ahead in this article. |
C | caret “^” marks the start of a pattern.^ may appear at the beginning of a pattern to require the match to occur at the very beginning of a line. For example, ^xyz matches xyz123 but not 123xyz. |
D | Dollar “$” marks the end of a pattern.$ may appear at the end of a pattern to require the match to occur at the very end of a line. For example, pqr$ matches 123pqr but not pqr123. |
Caret (^) and dollar sign ($) indicate the pattern to the beginning or end of the string being searched.The two anchors may be combined. For example, ^pqr$ matches only pqr. Any characters after or before it will make the pattern invalid.
Let’s start with the first validation, enter character which exists between a-g?
Hide Copy Code
[a-g]
Enter characters between [a-g] with length of 3?
Hide Copy Code
[a-g]{3}
Enter characters between [a-g] with maximum 3 characters and minimum 1 character?
Hide Copy Code
[a-g]{1,3}
How can I validate data with 8 digit fix numeric format like 91230456, 01237648 etc?
Hide Copy Code
^[0-9]{8}$
How to validate numeric data with minimum length of 3 and maximum of 7, ex -123, 1274667, 87654?
We need to just tweak the first validation with adding a comma and defining the minimum and maximum length inside curly brackets.
Hide Copy Code
^[0-9]{3,7}$
Validate invoice numbers which have formats like LJI1020, the first 3 characters are alphabets and remaining is 8 length number?
First 3 character validation |
Hide Copy Code
^[a-z]{3}
|
8 length number validation |
Hide Copy Code
[0-9]{8} |
Now butting the whole thing together.
Hide Copy Code
^[a-z]{3}[0-9]{7}$
Check for format INV190203 or inv820830, with first 3 characters alphabets case insensitive and remaining 8 length numeric?
In the previous question the regex validator will only validate first 3 characters of the invoice number if it is in small letters. If you put capital letters it will show as invalid. To ensure that the first 3 letters are case insensitive we need to use ^[a-zA-Z]{3} for character validation.
Below is how the complete regex validation looks like.
Hide Copy Code
^[a-zA-Z]{3}[0-9]{7}$
Validate numbers are between 0 to 25
Hide Copy Code^(([0-9])|([0-1][0-9])|([0-2][0-5]))$Validation for only numbers"^[0-9]*$"
To get MM/DD/YYYY use the following regex pattern.Hide Copy Code^([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$And finally to get YYYY/MM/DD use the following regex pattern.Hide Copy Code^(1[9][0-9][0-9]|2[0][0-9][0-9])[- / .]([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$Short cuts
You can also use the below common shortcut commands to shorten your regex validation.
Actual commands | Shortcuts |
[0-9] | \d |
[a-z][0-9][_] | \w |
O or more occurrences | * |
1 or more occurrences | + |
0 or 1 occurrence | ? |