Regular expressions are patterns used to match character combinations in strings.
Sometimes while testing we come across character validation scenarios.
Few common cases of regex are:
- Validating email pattern
- Validating particular pattern or only few set of character for a field
- searching for a pattern of string from text result.
- Restricting some characters to be entered via UI and API as part of security protocols.
We can make use of regex here to allow or restrict characters.
Lets take some simple scenario to understand how this works.
Usecase1:
A text field which allows the users to enter below range of characters only
a to z , A to Z , 0 to 9
Now lets construct regex to allow only these ranges of characters.
1- To start any regex expression we need to start with the char "^"
2 - Now to include the range or pattern we need to enclose it with [] brackets
3- Now for a to z , the pattern will be a-z
4- for A to Z pattern will be A-Z
5 - For 0 to 9 pattern will be 0-9 or \d
6- Finally our regex will be : ^[a-zA-Z0-9]
To test it, we can use online regex tester website:
regexr.com

You can that the given text matches as per the regex pattern we gave.
But there is a problem here.
If we try to add any character which is not within this allowed range from 2nd char position and in anywhere of the text , it will still be valid.

Notice the characters we added at the end of the text. We still get match but we did not allow it. Then how is it matching?
Because we have specified start of line as ^ but we did not specify if this regex should be applied to all the characters till the end of the text.
So it only checks the 1st char and validates as per the regex.
From the 2nd char on wards whatever you type will be accepted.
How to let it validate for entire string?
We can introduce the end of line character sequence +$
So the updated regex will now be : ^[a-zA-Z0-9]+$
This will now check for the regex for the entire input text.

Now see the result, it has no match.
Now lets see some more interesting use cases.
Usecase2: Allow below ranges
| Range | Regex |
| g to q | g-q |
| F to K | F-K |
| 5 to 9 | 5-9 |
| dot ( .) | . |
| forward slash (/) | / |
| backward slash (\) | \\\\ |
| Double quote ( ” ) | \” |
| space | (space char) |
| square brackets [] | \[\] |
Out of all the above , square brackets ,backward slash and double quote are the tricky ones as these are escape characters and we need to wrap them with backslash to escape.
Interesting thing to notice here is escaping backward slash requires a bit of technical understanding.
\\ – 1st slash is to escape the 2nd one
\\\ – 1st slash will escape the 2nd one, now it will become \\ and again it will escape the 2nd one. and it will become \
\\\\ – 1st one will escape 2nd and 3rd will escape 4th and we will get \\ finally.
So to get one backward slash after escaping we need four backward slashes.
Now lets construct our full regex:
Start and end characters with enclosing: ^[...here we wil keep all chars based on range]+$
Final regex:
^[g-qF-K5-9./\\\\\" \[\]]+$

And we see it matches with all these range of characters.
Try yourself with some characters out of the given range and see what happens.
I hope this helps you understand the basic , you can study more on complex regex further.
Check this article for more complex regex :
https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html
Do let us know your feedback.
Learn, Explore , Test , Share …..