Regex? The Minimum You Need To Know.
Tags: regex, programming, validations
Published: 2021-04-06 21:59:35

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 :point_down:	

![Regex Use Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cpv19e34kfiyios6xvh.PNG)

These are places where **REGEX** is used.

## What Is Regex?
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.

## How To Use Regex? 
You can use different functions for matching regex with your data. In PHP there are functions starting with `preg_` that mostly match **regex** (see **[Regex for Php](https://www.php.net/manual/en/ref.pcre.php)**) while In JavaScript, regex are also objects. You can see some of the regex functions for JavaScript on **[Javascript Regex Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)**.

## Basic Validations
There are a number of online **regex engines** that you can use to quickly test out your regex expression. I mostly use **[Regex101](https://regex101.com/)** because I like it. The following examples are tested on Regex101.

##### Email
`/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/`

![Email Validation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmxf8wqsn80ul9244k6k.PNG)

##### Password
Minimum eight characters, at least one uppercase letter, one lowercase letter, and one number

`/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/`

![Password Validation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl9m9p3namsm2mt0fod2.PNG)

For more password validations, Check out the **[answer on stackoverflow](https://stackoverflow.com/a/21456918/12579290)**


##### Numbers Only
To match 0 or many numbers
`/^[0-9]*$/`
![Numbers 0 or many](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvwwk8gfcskyh93db8sp.PNG)
To match 1 or many numbers
`/^[0-9]+$/`
![Numbers 1 or many](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvcydj8giznhsdbyk33r.PNG)
To match exactly 1 number
`/^[0-9]$/`
![Numbers exactly 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umhr6zjh4ape2r2zhjgd.PNG)
If you add one more number in the test string, the validation will fail.

### Phone Numbers Only
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**
`/^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$/`
![Phone Number validation country code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23ual8brwv9o6sjhdeh7.PNG)
It validates the phone number with and without the country code like the following do pass

- 00923000000000
- +923000000000
- 03000000000
 

I would suggest that for a good reference, check out **[Google's Library](https://github.com/google/libphonenumber)** for validating international phone numbers. This is not regex but it gets the job done if are on the run.

#### Characters Only
To match for 1 or many characters
`/^[A-Za-z]+$/`
![Characters only 1 or many](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5sn7qtel8j4ewiyx0yt.PNG)

- To match 0 or many characters you can use `/^[A-Za-z]*$/`
- To match exactly 1 character you can use `/^[A-Za-z]$/`

Of course, there are other combinations you can explore.

#### URL Matching
The following will mostly match the URLs you want to
`^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$`
![URL Matching exmaple](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap47bo7izyspogfky8mt.PNG)

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](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url)** that has different implementations of Php and Js that you can play with.

## Which Language Supports Regex?
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.

## Now Some Theory.
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 :point_down:

**A Regex can have**
- Tokens
- Anchors
- Meta Sequences
- Quantifiers
- Group Constructs
- Character Classes
- Flag/Modifiers
- Substitution

If you want to learn and practice them at the same time then I would again suggest, goto **[Regex101](https://regex101.com/)**. It has a good reference for the **regex** operators. :point_down:
![Regex101 regex reference](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb6b7znp12zpuxriy0au.PNG) and I think I don't need to explain more :wink:

### Fun Part While Writing The Article
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](https://en.wikipedia.org/wiki/Urdu)** it matched `اِسلامی جمہوریہ پاكِستان`

![Regex that matches every word of every language](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cwcgmo0ldd3vwn1cebn.PNG)
I don't know if it's useful or not, but I will add it in the comments.

### Tip
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](https://www.w3schools.com/html/html_form_input_types.asp)** for validating some form fields on the frontend, but for the backend you need **regex.**

---
### Conclusion
So did you use regex for a complex match sometime or you just got to know about it? Also, give a :sparkling_heart: or a :unicorn: if you like the article.