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

How to match at the beginning of string in python using Regular Expression?


The following code matches the word 'cheer' at the beginning of the string ' cheer leaders at the football stadium'

^-matches the beginning of the string

Example

import re
s = 'cheer leaders at the football stadium'
result = re.search(r'^\w+', s)
print result.group()

Output

This gives the output

cheer