use of \Z in regular expresion in python by R4R Team

Z : Returns a match if the specified characters are at the end of the string

Example-

re.search('StartZ',"string Start") will match the pattern.


program-

#import the re module
import re

#take input of string
print("Enter string")
s=input()

#check string is ends with 'end'
x=re.search('endZ',s)
if x:
print("String is ends with 'end'")
else:
print("String is not ends with 'end'")


#check string is ends with 'Finish'
x=re.search('FinishZ',s)
if x:
print("String is ends with 'Finish'")
else:
print("String is not ends with 'Finish'")


output-

Enter string
This is end
String is ends with 'end'
String is not ends with 'Finish'

Enter string
string is Finish
String is not ends with 'end'
String is ends with 'Finish'


-In this program, we take a input of string, and check that the string is ends with 'end' or not, and in other part we check string is ends with 'Finish' or not.
Because of the 'Z', we only focus on the Ending string, and whatever in the starting in the string are not reflect on the match pattern.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!