Importance of Regex

Durgasomeswararao
2 min readJul 25, 2020

Generally, people used to write a huge amount of code by adding multiple conditional statements, string operations, and looping to detect a small word, sentence, or line from large huge data.

Let’s start our regex journey to avoid this :)

Regex world

Is it really important to use regex?

Let’s think about how you can solve this small below use case

Google is working with Artificial Intelligence products

Now let’s play with below sentences matching in application :)

Google is working with Artificial Intelligence

Google is working with Intelligence product

Google working with Artificial Intelligence

Google is working with Artificial Intelligence product

Google working with Artificial Intelligence products

We can cover all the above sentences with below single line code in :)

Google\s*(is)?\s*\s*working\s*with\s*(Artificial)?\s*Intelligence\s*(product)?s?

Now let’s take above use case to next level of complexity and solve yourself :)

Google is working with Artificial Intelligence products

Microsoft is working with Artificial Intelligence products

Now apply the above use case and try with some more company names.

Advantages

Code optimization

Performance improvement

One line statement for many use cases

Detecting valid content without processing data manually etc

Here are some of the regex implemented to detect patterns from bulk amounts of data with one single statement of code :)

https?://(www2?\.)?(([A-Za-z0–9-]+\.)+[A-Za-z0–9]+) — Detecting a valid Domain name

(?<=\<a)(\s)+href=\”.*(?=\” \b)(?<=.){1,} — Detecting links from web pages

^((2[0–5]?[0–5]?|[0–1]?[0–9]?[0–9]?)\.){3}(2[0–5]?[0–5]?|[0–1]?[0–9]?[0–9]?)$|^(([0–9a-f]{1,4}:){7}[0–9a-f]{1,4})$ — Detecting IPv4/IPV6

((?<=\<\/)|(?<=\<))(\s)*(\w)+(?=.*\>) — Web page tags Detection

--

--