Python regex capture whole integer


I am trying to extract several parts of a string from a log file. I can match the number I want, but only the first digit. There is a related question here, but it tries the opposite: matching only the beginning of an integer.

Here is a minimal working example:

import re
regex =
                "adding 2000 to database, removing 3000")
if regex:
    print("Regex matched!")
    print("Line number : {}".format("line_number")))
    print("Regex didn't match!")

Line number : 0
Line number : 2000

Asked By: xancho



.* at start and in the middle of your regex consumes anything including digits (the last one is still matched so the regex engine respects the "one digit or more" condition).

You have to exclude digits. Using D does exactly that.

regex =
                "adding 2000 to database, removing 3000")
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.