1.

Which of the codes shown below results in a match?(a) re.match(‘George(?=Washington)’, ‘George Washington’)(b) re.match(‘George(?=Washington)’, ‘George’)(c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)(d) re.match(‘George(?=Washington)’, ‘Georgewashington’)I had been asked this question in my homework.This intriguing question originated from Regular Expressions topic in chapter Regular Expressions and Files of Python

Answer»

Right option is (c) re.match(‘GEORGE(?=Washington)’, ‘GeorgeWashington’)

BEST explanation: The code SHOWN above demonstrates the use of the function re.match, alongwith the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the MODULE to IGNORE case. Hence the match is case-sensitive. Therefore the only option which results in a match is:

re.match(‘George(?=Washington)’, ‘GeorgeWashington’)



Discussion

No Comment Found

Related InterviewSolutions