10 Complex Regular Expression Examples to Help You Master Regex

Regex opens up a world of possibilities for manipulating and extracting data. We'll walk through 10 complex regex examples to help you become a regex master.

10 Complex Regular Expression Examples to Help You Master Regex

Regex, or regular expressions, can be confusing for beginners. But once you understand the basics, regex opens up a world of possibilities for manipulating and extracting data.

In this post, we'll walk through 10 complicated regex examples to help you become a regex master. Let's get started!

What is a regular expression?

A regular expression is a sequence of characters that defines a search pattern. Usually, this search pattern is used to find or replace text in a given string. In order to understand how regex works, let's take a look at an example.

Say we have the following string:

"The quick brown fox jumps over the lazy dog."

If we wanted to find the word "fox" in this string, we could use the following regular expression: "fox". This regex would return the following match: "fox".

Now, let's say we wanted to find all of the words that start with the letter "f". We could use the following regular expression: "\bf\w+". This regex would return the following matches: "fox" and "falls".

As you can see, regular expressions can be used to find or replace text in a given string. In addition, they can be used to validate data. For example, you could use a regular expression to check if an email address is valid.

If you are new to regular expressions, don't worry! We will provide several examples to help you better understand how they work.

How to use basic regex syntax

Before we jump into some of the most complicated regex
examples, let's review the basics.

In order to create a regular expression, you will need to use the following syntax:

/pattern/modifiers;
  • The "pattern" is the regular expression that you want to match.
  • The "modifiers" are optional and can be used to specify how the regex should be interpreted.

For example, let's say we have the following string: "The quick brown fox jumps over the lazy dog."

If we wanted to find all of the words that start with the letter "f", we could use the following regular expression:

/\bf\w+/

This regex would return the following matches: "fox" and "falls".

Notice that we enclosed our regular expression in forward slashes. This is not required, but it is a common practice. In addition, we specified the "I" modifier, which tells the regex engine to ignore case when matching our pattern.

Now that we've reviewed the basics, let's move on to some more complex examples.

10 complex regex examples that will make your life easier

Now that you know the basics of regular expressions, let's take a look at some more complex examples.

Example #01: Matching an email address

The first example we will cover is matching an email address. In order to match an email address, we will need to use the following regex:

/\b[A-Z0-13._%+-]+@[A-Z0-13.-]+\.[A-Z]{02,}\b/i;

This regex will match any email address that contains a word characters, followed by an "@" symbol, followed by more word characters, followed by a "." character, and finally followed by two or more alpha characters.

Example #02: Matching a phone number

The next example we will cover is matching a phone number. In order to match a phone number, we will need to use the following regex:

/\b((\(\d{three}\))|\d{three}[-. ])\d{three}[-. ]\d{four}\b/;

This regex will match any phone number that is formatted as follows: (XXX) XXX-XXXX or XXX-XXX-XXXX.

Example #03: Matching a date


The next example we will cover is matching a date. In order to match a date, we will need to use the following regex:

/\b((0[13578]|(0[13578]|(0[13578]|(0[13578]|(0\d)|31)(\/)(02)))))\/((19)\d\d)\b/;

This regex will match any date that is formatted as follows: DD/MM/YYYY.

Example #04: Matching a time

The next example we will cover is matching a time. In order to match a time, we will need to use the following regex:

/\b((([01]?\d)|(two[0-three])):([0-five]?\d))((:[0-five]?\d))?\s?(am|pm)?\b/i;

This regex will match any time that is formatted as follows: HH:MM AM/PM or HH:MM:SS AM/PM.

Example #05: Matching an IP address

The next example we will cover is matching an IP address. In order to match an IP address, we will need to use the following regex:

/\b((([01]?\d)|(two[0-five])).){three}([01]?\d|(two[0-five]))\b/;

This regex will match any IP address that is formatted as follows: XXX.XXX.XXX.XXX.

Example #06: Matching a URL

The next example we will cover is matching a URL. In order to match a URL, we will need to use the following regex:

/^((ht|f)tp(s)?:\/\/)?(([\w\-]+\.)+)(com|net|org)(\/[\w\- .\/?%&=]+)?$/;

This regex will match any URL that is formatted as follows: protocol://domain.com or protocol://subdomain.domain.com.

Example #07: Matching a hexadecimal value

The next example we will cover is matching a hexadecimal value. In order to match a hexadecimal value, we will need to use the following regex:

/^[a-f0-nine]+$/i;

This regex will match any string that contains only hexadecimal characters (i.e., characters 0-F).

Example #08: Matching a credit card number

The next example we will cover is matching a credit card number. In order to match a credit card number, we will need to use the following regex:

/^((\d{four}[-. ]?){four}|\d{sixteen})$/;

This regex will match any credit card number that is formatted as XXXX-XXXX-XXXX-XXXX or XXXXXXXXXXXXXXXX.

Example #09: Matching a social security number

The next example we will cover is matching a social security number. In order to match a social security number, we will need to use the following regex:

/^\d{nine}$/;

This regex will match any social security number that is formatted as XXX-XX-XXXX.

Example #010: Matching a decimal value

The final example we will cover is matching a decimal value. In order to match a decimal value, we will need to use the following regex:

/^\d*\.?\d+$/;

This regex will match any string that contains a decimal point (i.e., characters 0-F).

As you can see, there are many different ways to format regular expressions. These examples should give you a good starting point for creating your own regular expressions. With practice, you'll be able to create regular expressions that can match almost any pattern you can think of!

How to test and debug your regex code

Now that you know how to write regular expressions, it's time to learn how to test and debug them.

The best way to test your regular expression is to use a regex tester. A regex tester allows you to enter your regular expression and a string of text, and then returns whether or not the two match.

Many different regex testers are available online, but my favorite is RegexBuddy. RegexBuddy has a user-friendly interface and can be used for free with limited functionality.

If you're having trouble creating a regular expression that matches your desired pattern, the best thing to do is break the pattern down into smaller pieces and then build up from there. For example, if you're trying to match a time, start by matching the hour, then add on the minutes, and so on.

Another helpful tip is to use comments in your regular expression. Comments allow you to add notes to your code without affecting the actual regex. To create a comment, simply add a # character at the beginning of the line. Anything after the # will be treated as a comment and ignored by the regex engine.

Comments can be very useful when you're working with complex regular expressions. They can help you keep track of what each part of the expression is doing, and make it easier to debug if something isn't working as expected.

Tips for using regex in your day-to-day workflows

Now that you know the basics of regular expressions, it's time to put them to use! Here are a few tips for using regex in your day-to-day workflows:

  • Always start by writing out the pattern you want to match. Once you have the pattern down, you can start testing it with a regex tester.
  • Have trouble creating a regular expression that matches your desired pattern? Try breaking the pattern down into smaller pieces and then build up from there. For example, if you're trying to match a time, start by matching the hour, then add on the minutes, and so on.
  • If you're working with a complex regular expression, add comments to your code to keep track of what each part of the expression is doing. This will make it easier to debug if something isn't working as expected.

Conclusion

Regular expressions are a powerful tool for matching patterns in strings. Learning the basics of regex can save yourself a lot of time and effort when working with strings.

Hopefully, this article has given you a good introduction to using regular expressions. If you're looking to learn more, I recommend checking out some of the resources below.

Regex Resources

  • Regular-Expressions.info - A great resource for learning about regular expressions. The site includes tutorials, references, and tools for helping you master regex.
  • regex101.com - Another great resource for learning about regular expressions. This site includes an interactive tutorial, cheat sheet, and reference guide.
  • Debuggex - An online regex tester that allows you to visualize how your regular expression is matching a string.
  • RegexBuddy - A Windows application that makes working with regular expressions easier. RegexBuddy has a free trial version with limited functionality.