forked from chavarera/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.BasicOfRegularExpression.py
More file actions
99 lines (73 loc) · 1.81 KB
/
1.BasicOfRegularExpression.py
File metadata and controls
99 lines (73 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''
A regular expression is a special sequence of
characters that helps you match or find other strings
or sets of strings, using a specialized syntax
held in a pattern. Regular expressions are widely used in UNIX world.
'''
'''
IDENTIFIRES:-
\d any number
\D anything except a number
\s space
\S anything except a space
\w any charcter
\W anything except a character
. any character but except new line
\b white space arround words
\. a period(this will find out . in given string)
MODIFIRES:-
{2,6} describing about numbers expecting result 2-6
+ Match 1 or More
? Match 0 or 1
* Match 0 or more
$ Match the end of string
^ Matching the begining of string
| either or (Example {1,3} | {6-9})
[] range or variance example [1-5],[A-Z]
{x} expecting "x" amount
White Spaces
\n new line
\t tab
\e escape
\s space
\f from feed
\r return
Note:
We can use diffrent solution for solving same problem
'''
import re #importing regular expression module
mystring='''
hi Akash is 67 years old,
and Ajay is 28 years old, Raj is 109'''
#find ages
'''
re is module name
findall is method
r is for regular expression
\d finding digit
{1,3} modifier to find 1 or 2 or 3 digit number
For Example
data='100 is number 5 where abcd@gmail.com is email. 54854584'
To find Exact 3 digit group use {3} after the \d
rex='\d{3}'
searchall=findall(rex,data)
output=['100','548','545']
To find 1,2,3, digit number use {1,3}
rex='\d{1,3}'
searchall=findall(rex,data)
output=['100','5','548','545','84']
'''
ages=re.findall(r'\d{1,3}',mystring)
print(ages)
# this will give output : ['67', '28', '109']
# Now Find out name
names=re.findall(r'[A-Z][a-z]*',mystring)
print(names)
#this will give output :['Akash', 'Ajay', 'Raj']
# save this output in dictonary
mydict={}
index=0
for name in names:
mydict[name]=ages[index]
index=index+1
print(mydict)