Python Regex Match ( ) function - Coding exercise
Test your Python Regex match() and group() function skills.
The Match Function Exercise
Let’s assume that we have a string variable called line
, where line = "Learn to Analyze Data with Scientific Python"
passed to a function called regex_processor()
in the following code.
Can you find the word "Analyze"
after the word "to"
?
import redef regex_processor(line):# Passed from the arg, line = "Learn to Analyze Data with Scientific Python"m = re.match( r'(.*) to (.*?) .*', line, re.M|re.I)# Replace the '0' with correct number (1 or 2?)if m:print (m.group(0))else:print ("No match!!")
Your task is to replace the '?'
on the line 9 with a correct group number (1
or 2
) for the word Analyze
.
Do you need a hint?
Click on the button below: