The hardest part is learning the syntax and learning how to write your own regex code from scratch. To save time, I have selected 30 different regex code snippets that you can use in your projects. And since regex is not limited to a particular language, you can apply the following paragraphs in any language from Javascript to PHP or Python.
1 The strength of the password
1 2 3 |
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$ |
Testing the strength of a password is often subjective so there is no absolute exact answer. But I feel that this regex is a great starting point if you don’t want to write your own password strength checker from scratch.
2 Hex Color Code
1 2 3 |
\#([a-fA-F]|[0-9]){3, 6} |
Hex color codes are very popular in the field of web development. This regex fragment can be used to retrieve the appropriate hex code from any string for any purpose.
3 Authentication email address
1 2 3 |
/[A-Z0-9._%+-]+@[A-Z0-9-]+.+.[A-Z]{2,4}/igm |
One of the most common tasks for a developer is to check whether a string is a valid email address or not. For PHP, you can use the function filter_var: filter_var ($ email, FILTER_VALIDATE_EMAIL)
4 Address IPv4
1 2 3 |
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/ |
Similar to an email address is the IP address – is used to identify a specific computer to access the Internet. This regular expression will check a string if it complies with the syntax of IPv4 addresses or not.
5 Address IPv6
1 2 3 |
(([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])) |
Or maybe you’ll want to check out an IPv6 address advanced regex syntax. The difference is very small, although it is important in the development process.
6 Thousands separator
1 2 3 |
/\d{1,3}(?=(\d{3})+(?!\d))/g |
This regex code works on any number and will apply any delimiter for each 3 digits separated by thousands, millions, …
7 Add HTTP before the link
1 2 3 4 5 |
if (!s.match(/^[a-zA-Z]+:\/\//)) { s = 'http://' + s; } |
Whether you’re working in JavaScript, Ruby or PHP, this expression could be very useful. It will check any URL string to see if it has the prefix HTTP / HTTPS or not, and if not, add this string to match.
8 Get domain from URL
1 2 3 |
/https?:\/\/(?:[-\w]+\.)?([-\w]+)\.\w+(?:\.\w+)?\/?.*/i |
Each domain name contains the site in the protocol (HTTP or HTTPS) and sometimes the path, subdomain with additional pages. You can use this regex segments to cut through all of that and just return the domain name.
9 Sorting keywords by counting the number of words
1 2 3 4 5 6 7 |
^[^\s]*$ matches exactly 1-word keyword ^[^\s]*\s[^\s]*$ matches exactly 2-word keyword ^[^\s]*\s[^\s]* matches keywords of at least 2 words (2 and more) ^([^\s]*\s){2}[^\s]*$ matches exactly 3-word keyword ^([^\s]*\s){4}[^\s]*$ matches 5-words-and-more keywords (longtail) |
Those who use Google Analytics and Webmaster Tools will really like this regular expression. It can sort the keywords based on the words use searching.
This may be a specific amount (ie only 5 words) or it can fit a variety of words (ie 2 or more). When used to sort and analyze the data, it is one of the most powerful regular expressions.
10 Find a valid Base64 string in PHP
1 2 3 |
\?php[ \t]eval\(base64_decode\(\'(([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?){1}\'\)\)\; |
If you’re a PHP developer, then sometimes you can analyze through the code to find objects Base64 encoded binary (eg looking shell hidden in a PHP). This regex segments can apply to all PHP code and will examine any of the existing Base64 string.
Take your time to comment on this article.