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

PythonServer Side ProgrammingProgramming

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
raja
Published on 02-Jan-2018 10:38:02
Advertisements