Skip to content

Commit 53e0e02

Browse files
Initial commit
0 parents  commit 53e0e02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5500
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.ipynb_checkpoints/
2+
__pycache__/

01. Introduction.ipynb

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# <center> RegEx in Python</center>\n",
8+
"\n",
9+
"![](images/memes/meme1.jpg)\n",
10+
"\n",
11+
"\n",
12+
"> ### *A regular expression is a sequence of characters that define a search pattern.*"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"## 1. What exactly is a Regular Expression?\n",
20+
"\n",
21+
"A regular expression, often called a pattern, is **an expression used to specify a set of strings** required for a particular purpose. \n",
22+
"\n",
23+
"- A simple way to specify a finite set of strings is to list its elements or members. <br>For example `{file, file1, file2}`. \n",
24+
" \n",
25+
"\n",
26+
"- However, there are often more concise ways to specify the desired set of strings. <br>For example, the set `{file, file1, file2}` can be specified by the pattern `file(1|2)?`. <br>We say that this pattern matches each of the three strings. [Wanna check?](https://regexr.com/48om5)\n",
27+
"\n",
28+
"> In most formalisms, if there exists at least one regular expression that matches a particular set then there exists an infinite number of other regular expressions that also match it, i.e. **the specification is not unique**.<br>\n",
29+
"For example, the string set `{file, file1, file2}` can also be specified by the pattern `file\\d?`.\n",
30+
"\n"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"\n",
38+
"## 2. The math of Regular Expressions\n",
39+
"\n",
40+
"- The concept of **Regular Expressions** originated from **[Regular Languages](https://en.wikipedia.org/wiki/Regular_language)**. \n",
41+
"\n",
42+
"- **Regular Expressions** describe **Regular Languages** in **[Formal Language Theory](https://en.wikipedia.org/wiki/Formal_language)**.\n",
43+
"\n",
44+
"> ***Formal Language Theory***: In mathematics, computer science, and linguistics, a **formal language** consists of words whose letters are taken from an alphabet and are **well-formed according to a specific set of rules**. The field of formal language theory studies primarily the purely syntactical aspects of such languages—that is, their internal structural patterns.\n",
45+
"\n",
46+
"> ***Regular Languages***: A regular language is a category of **formal languages** which can be expressed using a regular expression. ![](images/formal-lang-theory.png)\n",
47+
"\n",
48+
"- **Note**: Today, many regular expressions engines provided by modern programming languages are augmented with features that allow recognition of languages that <span style=\"color:red;\">**cannot**</span> be expressed by a classic regular expression!\n"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"## 3. Uses of Regular Expressions\n",
56+
"\n",
57+
"Some important usages of regular expressions are:\n",
58+
"\n",
59+
"- Check if an input honors a given pattern; for example, we can check whether a value entered in a HTML formulary is a valid e-mail address\n",
60+
"\n",
61+
"\n",
62+
"- Look for a pattern appearance in a piece of text; for example, check if either the word \"color\" or the word \"colour\" appears in a document with just **one scan**\n",
63+
"\n",
64+
"\n",
65+
"- Extract specific portions of a text; for example, extract the postal code of an address\n",
66+
"\n",
67+
"\n",
68+
"- Replace portions of text; for example, change any appearance of \"color\" or \"colour\" with \"red\"\n",
69+
"\n",
70+
"\n",
71+
"- Split a larger text into smaller pieces, for example, splitting a text by any appearance of the dot, comma, or newline characters"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"## 4. A brief history of Regular Expressions\n",
79+
"\n",
80+
"> *The story begins with a neuroscientist and a logician who together tried to understand how the human brain could produce complex patterns using simple cells that are bound together.*\n",
81+
"\n",
82+
"- In 1943, neurophysiologists ***Warren McCulloch*** and ***Walter Pitts*** published ***\"A logical calculus of the ideas immanent in nervous activity\"***. This paper not only represented the beginning of the regular expressions, but also proposed the first mathematical model of a neural network.\n",
83+
"\n",
84+
"\n",
85+
"- In 1956, ***Stephen Kleene*** wrote the paper ***\"Representation of events in nerve nets and finite automata\"***, where he coined the terms **regular sets** and **regular expressions** and presented a simple algebra.\n",
86+
"\n",
87+
"\n",
88+
"- In 1968, the Unix pioneer ***Ken Thompson*** took Kleene's work and extended it, publishing his studies in the paper ***\"Regular Expression Search Algorithm\"***. Ken Thompson's work didn't end in just writing a paper. He also implemented Kleene’s notation in the editor ***QED***. The aim was that the user could do advanced pattern matching in text files. The same feature appeared later on in the editor ***ed***.\n",
89+
"\n",
90+
"> To search for a Regular Expression in ed you wrote `g/<regular expression>/p` The letter g meant global search and p meant print the result. The command — `g/re/p` — resulted in the standalone program grep, released in the fourth edition of Unix 1973.<br><span style=\"color:red;\">However, **grep** didn’t have a complete implementation of regular expressions.</span>\n",
91+
"\n",
92+
"- In 1979, ***Alfred Aho*** developed ***egrep (extended grep)*** in the seventh edition of Unix. The program egrep translated any regular expressions to a corresponding [DFA](https://en.wikipedia.org/wiki/Deterministic_finite_automaton).\n",
93+
"\n",
94+
"\n",
95+
"- In 1987, ***Larry Wall*** created the scripting language ***Perl***. Regular Expressions are seamlessly integrated in Perl, even with its own literals. Hence, Perl pushed the regular expressions to the mainstream. The implementation in Perl went forward and added many modifications to the original regular expression syntax, creating the so-called ***Perl flavor***.\n",
96+
"\n",
97+
"### Some other worth mentioning milestones\n",
98+
"\n",
99+
"- The IEEE thought their POSIX standard has tried to standardize and give better Unicode support to the regular expression syntax and behaviors. This is called the ***POSIX flavor*** of the regular expressions.\n",
100+
"\n",
101+
"\n",
102+
"- In late 1980s, ***Henry Spencer*** wrote ***\"regex\"***, a widely used software library for regular expressions in C programming langauge.\n",
103+
"\n",
104+
"\n",
105+
"### Here is a brief timeline to summarize...\n",
106+
"\n",
107+
"![](images/history.png)\n",
108+
"\n",
109+
"### Regex today\n",
110+
"\n",
111+
"- It was the rise of the web that gave a big boost to the Perl implementation of regex, and that's where we get the modern syntax of regular expressions today; it really comes from Perl. `Apache`, `C`, `C++`, `the .NET languages`, `Java`, `JavaScript`, `MySQL`, `PHP`, `Python`, `Ruby` all of these are endeavoring to be Perl-compatible languages and programs. There's also a library called the `PCRE` library that stands for Perl-Compatible Regular Expression library.\n",
112+
"\n",
113+
"\n",
114+
"- Today, the standard Python module for regular expressions—`re`—supports only Perl-style regular expressions. There is an [effort](https://pypi.python.org/pypi/regex) to write a new regex module with better POSIX style support. This new module is intended to replace Python's `re` module implementation eventually. "
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"## 5. Understanding the Regular Expression Syntax\n",
122+
"\n",
123+
"A regex pattern is a simple sequence of characters. The components of a regex pattern are:\n",
124+
"\n",
125+
"- **literals (ordinary characters)**: these characters carry no special meaning and are processed as it is.\n",
126+
"\n",
127+
"- **metacharacters (special characters)**: these characters carry a special meaning and processed in some special way.\n",
128+
"\n",
129+
"\n",
130+
"![](images/components.png)\n",
131+
"\n",
132+
"Let's start with a simple example.\n",
133+
"\n",
134+
"Consider that we have got the list of several filenames in a folder.\n",
135+
"\n",
136+
"```\n",
137+
"file1.xml\n",
138+
"file1.txt\n",
139+
"file2.txt\n",
140+
"file15.xml\n",
141+
"file5.docx\n",
142+
"file60.txt\n",
143+
"file5.txt\n",
144+
"```\n",
145+
"\n",
146+
"And we want to filter out only those filenames which follow a specific pattern, i.e. `file<one or more digits>.txt`.\n",
147+
"\n",
148+
"> Let's try to do this on an online tool to learn, build, & test Regular Expressions (RegEx / RegExp), [RegExr](https://regexr.com).\n",
149+
"\n",
150+
"So, the regular expression we need here is:\n",
151+
"\n",
152+
"`file\\d+\\.txt`\n",
153+
"\n",
154+
"This expression can be understood as follows:\n",
155+
"\n",
156+
"- `file` is a substring of literals which are matched with the input as it is.\n",
157+
"\n",
158+
"- `\\d` is a metacharacter which instructs the software to match this position with a digit (0-9).\n",
159+
"\n",
160+
"- `+` is also a metacharacter which instructs the software to match one or more iterations of the preceeding character (`\\d` in this case)\n",
161+
"\n",
162+
"- `\\.` is a literal. `.` is a metacharacter but we want to use it as a literal in this case. Hence, we escape it using `\\` character.\n",
163+
"\n",
164+
"- `txt` is a substring of literals which are matched with the input as it is.\n",
165+
"\n",
166+
"![](images/example1.png)"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"![](images/memes/meme2.jpg)"
174+
]
175+
}
176+
],
177+
"metadata": {
178+
"kernelspec": {
179+
"display_name": "Python 3",
180+
"language": "python",
181+
"name": "python3"
182+
},
183+
"language_info": {
184+
"codemirror_mode": {
185+
"name": "ipython",
186+
"version": 3
187+
},
188+
"file_extension": ".py",
189+
"mimetype": "text/x-python",
190+
"name": "python",
191+
"nbconvert_exporter": "python",
192+
"pygments_lexer": "ipython3",
193+
"version": "3.6.7"
194+
}
195+
},
196+
"nbformat": 4,
197+
"nbformat_minor": 2
198+
}

0 commit comments

Comments
 (0)