Matches differs in re.search(and re.findall())

  Kiến thức lập trình

In Python 3.x code

import re
link = 'http://py4e-data.dr-chuck.net/known_by_Tamlah.html'
print(re.search('by_(.*).h', link).group())
print(re.findall('by_(.*).h', link))

I’m trying to find a name in the link.
Result of work.

by_Tamlah.h
[‘Tamlah’]

Regex the same, functions different. The question is why re.search() function doesn’t understand string extraction start, end regular expression ()? The correct result is Tamlah.

New contributor

ElIgo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT