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