You might have noticed when filling out an online form for email or password, you sometimes get validation errors like email must be valid or password must be 8 digits long. Something like this 👇
These are places where REGEX is used.
Regex is short for Regular Expressions.
A sequence of characters that specifies a search pattern. These patterns are mostly used by string-searching algorithms to find or find and replace character/s. Thus they can be used for validations and mostly you will be using Regex for them.
You can use different functions for matching regex with your data. In PHP there are functions starting with
There are a number of online regex engines that you can use to quickly test out your regex expression. I mostly use Regex101 because I like it. The following examples are tested on Regex101.
Minimum eight characters, at least one uppercase letter, one lowercase letter, and one number
For more password validations, Check out the answer on stackoverflow
This is a little difficult to explain as different countries have different phone numbers, codes, etc., you know what I am talking about.
For my number, and I have an 11 digit number(Pakistani Phone Number), I use the following regex
It validates the phone number with and without the country code like the following do pass
I would suggest that for a good reference, check out Google's Library for validating international phone numbers. This is not regex but it gets the job done if are on the run.
- To match 0 or many characters you can use
- To match exactly 1 character you can use
Of course, there are other combinations you can explore.
The following will mostly match the URLs you want to
This however does not match port numbers, Like it would not match
https://regex101:9000.com/?page=1. So you should have a general idea of what type of URL you want to match.
I suggest seeing Regex For URL that has different implementations of Php and Js that you can play with.
Almost every major language has support for regex.
To be more clear, implementations of regex functionality are called a regex engine, and a number of libraries are available for reuse which different languages do use.
Regex syntax may vary slightly between languages but for the most part, it is the same.
Fun is over guys, now we are moving towards the theory. Jokes aside, Most of you don't need to know everything about regex in detail, the same with a programming language, we can't learn everything about them.
But you should know that 👇
A Regex can have
- Meta Sequences
- Group Constructs
- Character Classes
If you want to learn and practice them at the same time then I would again suggest, goto Regex101. It has a good reference for the regex operators. 👇
and I think I don't need to explain more 😉
I found a regex from StackOverflow that was matching non-ASCII characters and it matched every word I tried of any language. Even in my native language Urdu it matched
اِسلامی جمہوریہ پاكِستان
More like a best practice. You should do regex validations on both the frontend and backend of your code. Just in case someone tries to manipulate the frontend, your backend will not validate the invalid data.
Note: You can also use HTML5 Input Types for validating some form fields on the frontend, but for the backend you need regex.
So did you use regex for a complex match sometime or you just got to know about it? Also, give a 💖 or a 🦄 if you like the article.