Skip to content

Commit 8e009e3

Browse files
Important Functions in re module like finditer(),sub(),subn(),split() and attributes like re.IGNORECASE.
1 parent 63af799 commit 8e009e3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

FunctioninreModule2.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Important Functions in re module of Python:
2+
''' finditer('RegEx',TargetString) =returns the callable Iterator of matched object
3+
sub(RegEx,replacement,target String)=returns the modified object after replacement if match found otherwise retuns None
4+
subn(RegEx,replacement,target String)=retuns the tuple with result String and number of times it replaced
5+
split('sepeartor',target String)=returns the list
6+
^a=starts with a
7+
a$=ends with a
8+
'''
9+
10+
import re
11+
matcher=re.finditer('[0-9]','abd2345@ 456dhjd')
12+
print("Digits found in Target String:")
13+
for m in matcher:
14+
print("starting index:{},group:{}".format(m.start(),m.group()))
15+
16+
s=re.sub('\\d','#','abcd12fnk45@cj')
17+
print("Replacing Digits with # symbol:",s)
18+
19+
t=re.subn('\\d','#','abcd12fnk45@cj')
20+
print(type(t))
21+
print("result String after replacing digits with #:",t[0])
22+
print("The number of replacement:",t[1])
23+
24+
l=re.split('-','10-20-30-40-50-60')
25+
print("List after split Operation:",l)
26+
27+
s="Learning Python is very easy"
28+
m=re.search('^Learn',s)
29+
if m!=None:
30+
print("Target String starts with Learn")
31+
else:
32+
print("Target String does not starts with Learn")
33+
34+
s="Learning Python is very easy"
35+
m=re.search('EASY$',s,re.IGNORECASE)
36+
if m!=None:
37+
print("Target String ends with easy")
38+
else:
39+
print("Target String does not ends with easy")
40+
41+
42+

0 commit comments

Comments
 (0)