Computer >> Computer tutorials >  >> Programming >> Python

How can I match the start and end in Python's regex?


We have the given string ‘TestCountry Hello’. To match and print that part of the string which starts and ends with characters ‘T’ and ‘y’ respectively we use the findall method of module re , word boundary anchor \b and non-whitespace character \S in the regex:

Example

import re
result = re.findall(r"\bT\S+y\b", 'TestCountry Hello')
print result

Output

to get output as

['TestCountry']