Skip to content

Commit d17c009

Browse files
Add files via upload
1 parent 3afeb99 commit d17c009

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed

Regular Expressions (regex).ipynb

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "6d16d334",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import re"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "98517c99",
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"Exist\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"text = \"This is Rahul Gupta !!! and this is a good day\"\n",
29+
"if re.search(\"good\",text):\n",
30+
" print(\"Exist\")\n",
31+
"else:\n",
32+
" print(\"not exist\")"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"id": "e94994da",
38+
"metadata": {},
39+
"source": [
40+
"# Tokenization"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 3,
46+
"id": "dcb3a943",
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"['',\n",
53+
" 'one can make a coffee . ',\n",
54+
" 'one can win the game . ',\n",
55+
" 'one can make it happen. ']"
56+
]
57+
},
58+
"execution_count": 3,
59+
"metadata": {},
60+
"output_type": "execute_result"
61+
}
62+
],
63+
"source": [
64+
"text = \"anyone can make a coffee . anyone can win the game . anyone can make it happen. \"\n",
65+
"re.split(\"any\",text)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 4,
71+
"id": "d79f0eec",
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"['any', 'any', 'any']"
78+
]
79+
},
80+
"execution_count": 4,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"re.findall('any',text)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 5,
92+
"id": "ac9f5620",
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"data": {
97+
"text/plain": [
98+
"<re.Match object; span=(0, 3), match='any'>"
99+
]
100+
},
101+
"execution_count": 5,
102+
"metadata": {},
103+
"output_type": "execute_result"
104+
}
105+
],
106+
"source": [
107+
"re.search('any',text)"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"id": "251a2da1",
113+
"metadata": {},
114+
"source": [
115+
"# Patterns"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 23,
121+
"id": "34b688b0",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"data": {
126+
"text/plain": [
127+
"['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']"
128+
]
129+
},
130+
"execution_count": 23,
131+
"metadata": {},
132+
"output_type": "execute_result"
133+
}
134+
],
135+
"source": [
136+
"grades = \"ABCAABCCCDAABBBBCCAABCAAABCCCCD\"\n",
137+
"re.findall('A',grades)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 10,
143+
"id": "53651790",
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"data": {
148+
"text/plain": [
149+
"['A',\n",
150+
" 'B',\n",
151+
" 'A',\n",
152+
" 'A',\n",
153+
" 'B',\n",
154+
" 'A',\n",
155+
" 'A',\n",
156+
" 'B',\n",
157+
" 'B',\n",
158+
" 'B',\n",
159+
" 'B',\n",
160+
" 'A',\n",
161+
" 'A',\n",
162+
" 'B',\n",
163+
" 'A',\n",
164+
" 'A',\n",
165+
" 'B']"
166+
]
167+
},
168+
"execution_count": 10,
169+
"metadata": {},
170+
"output_type": "execute_result"
171+
}
172+
],
173+
"source": [
174+
"re.findall('[AB]',grades)"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": 16,
180+
"id": "031ea978",
181+
"metadata": {},
182+
"outputs": [
183+
{
184+
"data": {
185+
"text/plain": [
186+
"['AB', 'AB', 'AB', 'AB', 'AB']"
187+
]
188+
},
189+
"execution_count": 16,
190+
"metadata": {},
191+
"output_type": "execute_result"
192+
}
193+
],
194+
"source": [
195+
"re.findall('[A][B]',grades)"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 19,
201+
"id": "a425cc1c",
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"data": {
206+
"text/plain": [
207+
"['AB', 'AB', 'CD', 'AB', 'AB', 'AB', 'CD']"
208+
]
209+
},
210+
"execution_count": 19,
211+
"metadata": {},
212+
"output_type": "execute_result"
213+
}
214+
],
215+
"source": [
216+
"re.findall('AB|CD',grades)"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 20,
222+
"id": "18f63cd0",
223+
"metadata": {},
224+
"outputs": [
225+
{
226+
"data": {
227+
"text/plain": [
228+
"['B',\n",
229+
" 'C',\n",
230+
" 'B',\n",
231+
" 'C',\n",
232+
" 'C',\n",
233+
" 'C',\n",
234+
" 'D',\n",
235+
" 'B',\n",
236+
" 'B',\n",
237+
" 'B',\n",
238+
" 'B',\n",
239+
" 'C',\n",
240+
" 'C',\n",
241+
" 'B',\n",
242+
" 'C',\n",
243+
" 'B',\n",
244+
" 'C',\n",
245+
" 'C',\n",
246+
" 'C',\n",
247+
" 'C',\n",
248+
" 'D']"
249+
]
250+
},
251+
"execution_count": 20,
252+
"metadata": {},
253+
"output_type": "execute_result"
254+
}
255+
],
256+
"source": [
257+
"re.findall('[^A]',grades)"
258+
]
259+
},
260+
{
261+
"cell_type": "markdown",
262+
"id": "7f7a1e84",
263+
"metadata": {},
264+
"source": [
265+
"# Quantifiers"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 24,
271+
"id": "c204afed",
272+
"metadata": {},
273+
"outputs": [
274+
{
275+
"data": {
276+
"text/plain": [
277+
"['AA', 'AA', 'AA', 'AAA']"
278+
]
279+
},
280+
"execution_count": 24,
281+
"metadata": {},
282+
"output_type": "execute_result"
283+
}
284+
],
285+
"source": [
286+
"re.findall('A{2,10}',grades)"
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": 31,
292+
"id": "9c357177",
293+
"metadata": {},
294+
"outputs": [
295+
{
296+
"data": {
297+
"text/plain": [
298+
"['AA', 'AA', 'AA', 'AAA']"
299+
]
300+
},
301+
"execution_count": 31,
302+
"metadata": {},
303+
"output_type": "execute_result"
304+
}
305+
],
306+
"source": [
307+
"re.findall('A{2,3}',grades)"
308+
]
309+
},
310+
{
311+
"cell_type": "code",
312+
"execution_count": 32,
313+
"id": "e8bcdf93",
314+
"metadata": {},
315+
"outputs": [
316+
{
317+
"data": {
318+
"text/plain": [
319+
"['AABCCCD', 'AAABCCCCD']"
320+
]
321+
},
322+
"execution_count": 32,
323+
"metadata": {},
324+
"output_type": "execute_result"
325+
}
326+
],
327+
"source": [
328+
"re.findall('A{1,10}B{1,10}C{1,10}D{1,10}',grades)"
329+
]
330+
},
331+
{
332+
"cell_type": "code",
333+
"execution_count": 40,
334+
"id": "5525b766",
335+
"metadata": {},
336+
"outputs": [
337+
{
338+
"name": "stdout",
339+
"output_type": "stream",
340+
"text": [
341+
"HELLO Rahul !!!\n"
342+
]
343+
}
344+
],
345+
"source": [
346+
"text = 'rahul@gmail.com'\n",
347+
"a = re.split(\"@\",text)\n",
348+
"name = a[0].capitalize()\n",
349+
"print(f\"HELLO {name} !!!\")"
350+
]
351+
}
352+
],
353+
"metadata": {
354+
"kernelspec": {
355+
"display_name": "Python 3 (ipykernel)",
356+
"language": "python",
357+
"name": "python3"
358+
},
359+
"language_info": {
360+
"codemirror_mode": {
361+
"name": "ipython",
362+
"version": 3
363+
},
364+
"file_extension": ".py",
365+
"mimetype": "text/x-python",
366+
"name": "python",
367+
"nbconvert_exporter": "python",
368+
"pygments_lexer": "ipython3",
369+
"version": "3.11.4"
370+
}
371+
},
372+
"nbformat": 4,
373+
"nbformat_minor": 5
374+
}

0 commit comments

Comments
 (0)