Introduction and History of Regular Expression
Tags: programming, automata, cpp, regex
Published: 2023-08-01 05:00:00

Hello World! Welcome to the "Regular Expressions" series, where we tackle the intimidating syntax that has spawned numerous memes among developers. Don't worry, though! As we move forward, I assure you that you'll gain the confidence to craft your own elegant regular expressions by the end of this journey.

> This cover image is taken from Midjourney.ai Discord server.

In this series, I will utilise https://regex101.com to share patterns with you, avoiding the constraint of a specific programming language that could potentially create barriers for some learners. 

> **Note** {% katex inline %} \to {% endkatex %} If anywhere coding is required, I will be using C++.

{% embed https://www.youtube.com/watch?v=79WVN-vGllU %}

## What are Regular Expressions?

Regular expressions are a specific type of **text pattern** that are used while programming a logic. 

I couldn't think of a single modern application that doesn't make use of it, either directly or indirectly. If you went to a website and entered any gibberish text in the email field, you would have received an invalid email format  message.

![invalid email](https://uploads-ssl.webflow.com/6115e9a52c1947532801e49d/611962b12526ce764890f2bd_Identify%20and%20describe%20errors%20to%20users.jpg)

> This image is taken from [here](https://www.accessguide.io/guide/identify-errors)

Under the hood, your input text is being verified by the following regex pattern. 

```
[a-z0-9.-]@[a-z0-9]{2,}\.[a-z]{2,}
```

You can see how this regex works [here](https://regex101.com/r/Rd09JD/1). This is a minimal version of matching simple email addresses. Please bear with me if this appears overwhelming. Such expressions can be created in less than 10 seconds.

### Why this name after all?

The name "regular expression" originates from the mathematical concept of [regular languages](https://en.wikipedia.org/wiki/Regular_language), which were first studied by mathematicians in the field of [formal language theory](https://en.wikipedia.org/wiki/Formal_language). Regular expressions are a way to describe and match patterns in strings of characters, hence the name "regular" expressions.

### Problem that Led Development of RegExp

It was created in the 1950s (far before most of us were born)  to help with _text processing_ tasks, such as _searching_ and _editing_. Since then, regular expressions have become a standard feature of many programming languages.

In the early 1960s, [Ken Thompson](https://en.wikipedia.org/wiki/Ken_Thompson) implemented regular expressions in the QED text editor. This was the first time that regular expressions were used in a practical application.

### Why You Should Learn Regular Expressions?

Learning regular expressions offers numerous benefits in various fields such as 
- data analysis
- software development
- content management
- scientific research

Their versatility allows you to define complex patterns for finding specific words or phrases, extracting data from structured text, and performing advanced search and replace operations. 

Additionally, mastering regular expressions helps prevent costly mistakes by providing a precise and controlled approach to text processing. With a solid understanding of regular expressions, you can confidently handle challenging text manipulation tasks, ensuring accuracy, reliability, and improved productivity.

Since you're here, I'm assuming you're ready to understand and use those unwieldy strings of brackets and question marks in your code.

## Flavours of RegExp

There is no established standard that specifies which text patterns are and are not regular expressions. There are numerous languages on the market whose creators have various ideas about how regular expressions should look. So we're now stuck with a whole spectrum of regular expression [_flavours_](https://www.regular-expressions.info/tutorial.html#engine) (implementation of RegExp in the programming language). 

But why reinvent the wheel? Instead, every modern regular expression engine may be traced back to the Perl programming language.

Regular expression flavours are commonly integrated into scripting languages, while other programming languages rely on dedicated libraries for regex support. JavaScript offers built-in support for regular expressions using the syntax `/expr/` or the `RegExp` object. On the other hand, Python implements regular expressions through its standard library [`re`](https://docs.python.org/3/library/re.html).

## References

{% embed https://en.wikipedia.org/wiki/Automata_theory %}

{% embed https://en.wikipedia.org/wiki/Regular_expression %}

{% embed https://en.wikipedia.org/wiki/Formal_language %}


## Contact Me

Email: tbhaxor _at_ proton _dot_ me
LinkedIn: `@tbhaxor`
Twitter: `@tbhaxor`