Most Important Regular Expressions

Hasan Özdemir
3 min readJan 20, 2022

--

Hello everyone, I hope your journey is going good with Computer Science.

RegEx is one of the most important headache for engineers. Today I will be briefly describing RegEx and then I will be sharing with you most common patterns that can rescue your life with your daily coding.

Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside many programming languages via packages. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like.

1. Validate Email Addresses

This pattern regarding to my tests work 99% of the all possible email forms.

2. Validate Telephone Numbers

I provide you solution with C# to make understand with other approaches. Detects most of the phone numbers all over the world. As you can imagine some countries and regions has extraordinary rules therefore before implement this code snippet to your project consider those conditions also.

3. Validate URLs

I provide you solution with Python to make you understand how it works in Python. This RegEx accepts HTTP and HTTPS protocols. You can check all conditions via this link

4. Validate Datetime

Dates come in various formats, depending on where you live in the world. In this example I provide most common three datetime formats regarding to your geolocation you can use it.

5. Validate IP Addresses

An Internet Protocol address (IP address) is a numerical label such as 192.0.2.1 that is connected to a computer network that uses the Internet Protocol for communication. I provide you IPv4 and IPv6 validation regarding your needs.

6. Validate Password

Junior, mid or senior somehow in your coding journey you encounter with passwords and discuss about how to make it safer than simple text.This RegEx checks that a password has a minimum of 6 characters, at least 1 uppercase letter, 1 lowercase letter, and 1 number with no spaces.

7. Validate JSON

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. In this RegEx we can check given json whether valid format or not.

8. Matching HTML Tags

HTML tags are enclosed in angle brackets < and >. To match the contents of any word enclosed in angle brackets (including non-standard tags), use the following expression, any if you want to match specific tags use another RegEx for your needs.

9. Validate Username

Username conditions can be different for each operation but end of the day most important case to understand the main logic. Let’s look at our example. Matches a string between 3 and 16 characters, allowing alphanumeric characters and hyphens and underscores

10. Matching Primitive Data

Primitive data structure is a kind of data structure that stores the data of only one type. Integer, float, string can be considered as primitive data type. In this code snippet we can see how to catch them from given text.

NOTES

  • In Python you must give pattern in this structure -> r’pattern’
  • In GoLang, Java you must give pattern in this structure -> ‘pattern’
  • Another important point is to give or not to give pattern between /(slash). For Python when I tested between slash it did not work out so please be careful when you pass pattern with slash.

RESOURCES

  • docs.python.org/3/
  • wikipedia.org/
  • developer.mozilla.org
  • docs.microsoft.com

--

--