Skip to content

Commit 5a58c62

Browse files
committed
Add note about regexp
1 parent 902819f commit 5a58c62

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

docs/python/language/regexp.ipynb

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "09c07f20",
6+
"metadata": {},
7+
"source": [
8+
"# Regular Expressions"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "e732250f",
14+
"metadata": {
15+
"vscode": {
16+
"languageId": "markdown"
17+
}
18+
},
19+
"source": [
20+
"Python regular expressions (regex) are compact pattern descriptions written in a special mini-language that let you search, match, extract, and transform substrings in text. In Python the built-in re module provides functions to compile and apply these patterns, supporting character classes, quantifiers, grouping, and lookaround assertions for powerful text processing."
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"id": "52df4454",
26+
"metadata": {},
27+
"source": [
28+
"## Module"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 1,
34+
"id": "48f23a79",
35+
"metadata": {
36+
"vscode": {
37+
"languageId": "markdown"
38+
}
39+
},
40+
"outputs": [],
41+
"source": [
42+
"import re"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"id": "d2a3c9d6",
48+
"metadata": {
49+
"vscode": {
50+
"languageId": "markdown"
51+
}
52+
},
53+
"source": [
54+
"## search()"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 12,
60+
"id": "f6ddbca4",
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"Match Found\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"import re\n",
73+
"txt = \"Python is a programming language\"\n",
74+
"x = re.search(\"^Python.*language$\", txt)\n",
75+
"if x is not None:\n",
76+
" print(\"Match Found\")\n",
77+
"else:\n",
78+
" print(\"Match not found\")"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"id": "e0670547",
84+
"metadata": {},
85+
"source": [
86+
"## findall()"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 14,
92+
"id": "20477ea4",
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"['i', 'i']\n"
100+
]
101+
}
102+
],
103+
"source": [
104+
"import re\n",
105+
"\n",
106+
"txt = \"Python is a programming language\"\n",
107+
"x = re.findall(\"i\", txt)\n",
108+
"print(x)"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"id": "7f8af177",
114+
"metadata": {},
115+
"source": [
116+
"## split()"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 25,
122+
"id": "03221334",
123+
"metadata": {},
124+
"outputs": [
125+
{
126+
"name": "stdout",
127+
"output_type": "stream",
128+
"text": [
129+
"['Python', 'is', 'a', 'programming', 'language']\n"
130+
]
131+
}
132+
],
133+
"source": [
134+
"import re\n",
135+
"\n",
136+
"txt = \"Python is a programming language\"\n",
137+
"x = re.split(r\"\\s\", txt)\n",
138+
"print(x)"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"id": "7f86aad6",
144+
"metadata": {},
145+
"source": [
146+
"## sub()"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": 26,
152+
"id": "0e767b5e",
153+
"metadata": {},
154+
"outputs": [
155+
{
156+
"name": "stdout",
157+
"output_type": "stream",
158+
"text": [
159+
"PythonXisXaXprogrammingXlanguage\n"
160+
]
161+
}
162+
],
163+
"source": [
164+
"import re\n",
165+
"\n",
166+
"txt = \"Python is a programming language\"\n",
167+
"x = re.sub(r\"\\s\", \"X\", txt)\n",
168+
"print(x)"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"id": "92b1c021",
174+
"metadata": {},
175+
"source": [
176+
"## Match Object"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 41,
182+
"id": "33cee649",
183+
"metadata": {},
184+
"outputs": [
185+
{
186+
"name": "stdout",
187+
"output_type": "stream",
188+
"text": [
189+
"Python is #\n"
190+
]
191+
}
192+
],
193+
"source": [
194+
"import re\n",
195+
"\n",
196+
"txt = \"Python is #1 programming language\"\n",
197+
"x = re.search(r\"\\D+\", txt)\n",
198+
"if x is not None:\n",
199+
" print(x.group())\n",
200+
"else:\n",
201+
" print(\"No match found\")\n"
202+
]
203+
}
204+
],
205+
"metadata": {
206+
"kernelspec": {
207+
"display_name": "notes (3.12.0)",
208+
"language": "python",
209+
"name": "python3"
210+
},
211+
"language_info": {
212+
"codemirror_mode": {
213+
"name": "ipython",
214+
"version": 3
215+
},
216+
"file_extension": ".py",
217+
"mimetype": "text/x-python",
218+
"name": "python",
219+
"nbconvert_exporter": "python",
220+
"pygments_lexer": "ipython3",
221+
"version": "3.12.0"
222+
}
223+
},
224+
"nbformat": 4,
225+
"nbformat_minor": 5
226+
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ nav:
2323
- Lists: python/language/lists.ipynb
2424
- Modules: python/language/modules.ipynb
2525
- Strings: python/language/strings.ipynb
26+
- Regular Expressions: python/language/regexp.ipynb
2627
- Exceptions: python/language/exceptions.ipynb
2728
- Tuples: python/language/tuples.ipynb
2829
- Dictionaries: python/language/dictionaries.ipynb

0 commit comments

Comments
 (0)