DEV Community

Cover image for Top 10 Most Used Regex Patterns
Albert Colom
Albert Colom

Posted on • Originally published at levelup.gitconnected.com on

Top 10 Most Used Regex Patterns

List of the most commonly used regular expressions that you should know

Regular expressions or regex are used to search for and match patterns in strings or text data. This is useful for tasks such as validating input, searching for specific words or phrases, and extracting information from text.

In this post I have tried to gather some of the most used examples, I hope you find them useful ;-)

1- Regex Email Validation

Regex pattern that matches a valid email address.

Example: name@domain.comhttps://regex101.com/r/1sIXyA/1

/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/
Enter fullscreen mode Exit fullscreen mode

2- Regex URL Validation

Regex pattern that matches a valid URL starting with http or https.

Example: https://www.domain.comhttps://regex101.com/r/gTO9nq/1

/^(https?:\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})(:[0-9]{1,5})?(\/.*)?$/
Enter fullscreen mode Exit fullscreen mode

3- Regex Dates Validation

Regex pattern that matches a valid date in different formats.

3.1- Date Format YYYY-mm-dd

Example: 2001–10-25https://regex101.com/r/xZOOo9/1

/^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/
Enter fullscreen mode Exit fullscreen mode

3.2- Date Format dd-mm-YYYY

Example: 25–10-2001https://regex101.com/r/p4pFsm/1

/^((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3})$/
Enter fullscreen mode Exit fullscreen mode

4- Regex Time Validation

Regex pattern that matches a valid time in different formats.

4.1- Time Format HH:mm AM/PM

Example: 11:45 PMhttps://regex101.com/r/vq9s8Z/1

/^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$/
Enter fullscreen mode Exit fullscreen mode

4.1- Time Format hh:mm:ss

Example: 19:45:54https://regex101.com/r/cMqbxL/1

/^(0[0-9]|1[0-9]|2[1-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])$/
Enter fullscreen mode Exit fullscreen mode

5- Regex Datetime Validation

Regex pattern that matches a valid datetime.

5.1- Datetime Format YYYY-mm-dd hh:mm:ss

Example: 2001–10-25 10:59:59https://regex101.com/r/GcnzIJ/1

/^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) (0[0-9]|1[0-9]|2[1-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]))$/
Enter fullscreen mode Exit fullscreen mode

5.2- Datetime Format dd-mm-YYYY hh:mm:ss

Example: 10–11-2001 10:59:59https://regex101.com/r/qfc0oc/1

/^((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3} (0[0-9]|1[0-9]|2[1-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]))$/
Enter fullscreen mode Exit fullscreen mode

6- Regex UUID Validation

Regex pattern that matches a valid UUID (Universal Unique Identifier).

Example: 20354d7a-e4fe-47af-8ff6-187bca92f3f9https://regex101.com/r/ESgo7B/1

/^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/
Enter fullscreen mode Exit fullscreen mode

7- Regex IP Address Validation

Regex pattern that matches a valid IP Address version 4 and version 6.

7.1- IPv4 address

Example: 127.3.1.1https://regex101.com/r/6kWr5c/1

/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ 
Enter fullscreen mode Exit fullscreen mode

7.2- IPv6 address

Example: ee1a:5b37:e33f:811d:1cc8:3607:af73:1e23https://regex101.com/r/4yy1CE/1

/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/
Enter fullscreen mode Exit fullscreen mode

8- Regex File Validation

Regex pattern that matches a valid file path.

8.1- Absolute file path with extension

Example: /some/path-to-fie/resource.ziphttps://regex101.com/r/3jdxKY/1

/^((\/|\\|\/\/|https?:\\\\|https?:\/\/)[a-z0-9_@\-^!#$%&+={}.\/\\\[\]]+)+\.[a-z]+$/
Enter fullscreen mode Exit fullscreen mode

8.2- File with extension having 3 chars

Example: resource.ziphttps://regex101.com/r/mRqdIg/1

/^[\w,\s-]+\.[A-Za-z]{3}$/
Enter fullscreen mode Exit fullscreen mode

8.3- File with extension validation

Example: resource.pnghttps://regex101.com/r/Rj980C/1

/^[\w,\s-]+\.(jpg|jpeg|png|gif|pdf)$/
Enter fullscreen mode Exit fullscreen mode

9- Regex Password Strength Validation

Regex pattern matching a strength rules of the password.

9.1- Complex

Should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long.

Example: mYpa$$word123https://regex101.com/r/mAC0uS/1

/^(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\-_+=~`|:;"'<>,.\/?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}$/
Enter fullscreen mode Exit fullscreen mode

9.2- Moderate

Should have 1 lowercase letter, 1 uppercase letter, 1 number, and be at least 8 characters long.

Example: passWord123https://regex101.com/r/7JBDjg/1

/^(?=(.*[0-9]))((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.{8,}$/
Enter fullscreen mode Exit fullscreen mode

10- Regex Slug Validation

Regex pattern that matches a valid Slug.

Example: some-valid-slughttps://regex101.com/r/0Bo0eH/1

/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Enter fullscreen mode Exit fullscreen mode

Original published at: albertcolom.com


Top comments (0)