-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25 - Regular Expression Modules in Python.py
More file actions
185 lines (80 loc) · 3.2 KB
/
25 - Regular Expression Modules in Python.py
File metadata and controls
185 lines (80 loc) · 3.2 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Regular Expression Modules in Python programming language :-
import re
# For importing this moduel we have to learn about some """METACHARACTERS"""
#. - any char
#[a-z A-Z] - char class a or b or c ... or z A or B or C .... or Z
#[0-9] - digit class 0 or 1 or 2 ..... or 9
#
#+ - atleast one [a-z]+
#* - Zero or More
#
#^ - Start of the string ( Caret or Circumflex )
#$ - End of the String
#
#? - Optional
#
#[a-z]{2,4} ( It will, print minimum of 2 & maximum of 4 characters )
#s
""" A example for applicability of RE Module:-"""
# Ex.-1) check for any particular PAN NO. that it's valid or not
s = "GWNPM4648K"
r = re.compile("[A-Z]{5}[0-9]{4}[A-Z]") #with [A-Z], there is no any curely bracket then by default it has acess of only 1 element
l = re.findall(r,s) # (r - indicates for reagular expression, while s - indicates for which we have to check )
print(l) #['GWNPM4648K']
# ( if the expression is valid then it will print the result otherwise not )
s = "GWNPM46485555K"
r = re.compile("[A-Z]{5}[0-9]{4}[A-Z]")
l = re.findall(r,s)
print(l) #[] shows FALSE by incicating empty brackets when RE Module is not matched
""" in the case when :- """
s = "AAAAAAGWNPM4648K"
r = re.compile("[A-Z]{5}[0-9]{4}[A-Z]")
l = re.findall(r,s)
print(l)# ['GWNPM4648K'] Still this is printing this correct PAN No. by taking initial 5 characters with further pattern, to avoding this we can use here "STARTING OF STRING META OPERATOR" )
s = "AAAAAAGWNPM4648K"
r = re.compile("^[A-Z]{5}[0-9]{4}[A-Z]")
l = re.findall(r,s)
print(l) # []
s = "GWNPM4648KZZZZZZZZ"
r = re.compile("[A-Z]{5}[0-9]{4}[A-Z]")
l = re.findall(r,s)
print(l)#['GWNPM4648K'] Still this is printing this correct PAN No. by taking initial characters with pattern, to avoding this we can use here "ENDING OF STRING META OPERATOR" )
s = "GWNPM4648KZZZZZZZZ"
r = re.compile("[A-Z]{5}[0-9]{4}[A-Z]$")
l = re.findall(r,s)
print(l)#[]
s = "AAAAAGWNPM4648K555555"
r = re.compile("^[A-Z]{5}[0-9]{4}[A-Z]$")
l = re.findall(r,s)
print(l)#[]
#Ex.2 - cHECK for any contact number that it's valid or not:-
s = "9669999880"
r = re.compile("[6-9][0-9]{9}")
l = re.findall(r,s)
print(l)# ['9669999880']
s = "55559669999880"
r = re.compile("[6-9][0-9]{9}")
l = re.findall(r,s)
print(l)#['9669999880']( For Avoiding this mistakes and getting the actual number use "INITIAL STRING CHARACTER"
s = "55559669999880"
r = re.compile("^[6-9][0-9]{9}")
l = re.findall(r,s)
print(l)#[]
s = "96699998809999999"
r = re.compile("[6-9][0-9]{9}")
l = re.findall(r,s)
print(l)#['9669999880'] ( Fot this case use "END STRING CHARACTETR )
s = "96699998809999999"
r = re.compile("[6-9][0-9]{9}$")
l = re.findall(r,s)
print(l)
#Ex.3 - check for the correct pattern of date by RE Module concept from pyton progtramming language
"""dd-mm-yyyy"""
s = "26-06-2002"
r = re.compile("^[0-9]{2}-[0-1][0-9]-[0-9]{4}$")
l = re.findall(r,s)
print(l)# ['26-06-2002']
s = "11126-05466-2002565"
r = re.compile("^[0-9]{2}-[0-9]{2}-[0-9]{4}$")
l = re.findall(r,s)
print(l)#[]