Search code examples
pythonregexnon-greedy

python regex non-greedy forced


I saw an example here about the non-greedy matching.

reg_string = "(.*?)>Title"
path = "<html><head><title>Title</title>"
match = re.match(reg_string, path)
if match:
   print match.group()

But what if I want to python to yell that this is not matching, because after the first > is no Title. Because this match:

"<html\><head><title>Title"

Solution

  • Try reg_string = "([^>]*?)>Title"