0

Using python I want to find the first year in a copyright statement. So, for example, if the content is

content = "Copyright 2010-2020 MyCompany"

and my regex is as follows:

REGEX = re.compile(r"(COPYRIGHT).+(\d{4}).+", re.I)

I expect the second group to be 2010, but it is 2020. Complete code:

content = "Copyright 2010-2020 MyCompany"
REGEX = re.compile(r"(COPYRIGHT).+(\d{4}).+", re.I)
m = REGEX.match(line)
print(m.group(2))

I guess the regex matched the most-right match. How to make it match the first match it finds?

What I expect is that the second match matches the year "2010" instead.

Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

Use the ungreedy ?

(COPYRIGHT).?(\d{4}).+
mike.k
  • 3,277
  • 1
  • 12
  • 18