Regex pattern no working in Python but on RegExr?

35 Views Asked by At

From the strings in data, I'm trying to capture "FUND", respectively "FUND*", "Total", and "Rest" in a first group, the following thereafter floating point number, including its prefixed sign, in a second group, and the contents between the parenthesis or brackets in a third group with my regex pattern in Python 3.

import re

if __name__ == "__main__":
    data = [
        "    - ‍♂️ FUND  +145.00 [ 2 reached]",
        "    -  FUND*  +25.00 (⟶ Mar. 75.00/300)",
        "    -  FUND   +17.49 (⟶ Apr. 300.00)",
        "    -  FUND   +36.21 (⟶ May 250.00)",
        "    -  FUND  +150.00 (⟶ Jul. 1500.00)",
        "    -  FUND  +115.00 (⟶ Sep. 1000.00)",
        "    - ✒️ FUND   +11.30",
        "      ----------------",
        "         Total  500.00",
        "         Rest     0.00"
    ]
    
    pattern = r"(\w+)\s+([-+]?\d*\.?\d+)\s?([:\(\[].+[:\)\]])?"
    for d in data:
        match = re.match(pattern, d)
        print(match)

I've tested the pattern on RegExr with the exact same data, and it works fine on the website. However, in Python match is always None?

Do I need to escape some characters in the pattern that I'm unaware of?

1

There are 1 best solutions below

0
julaine On BEST ANSWER

re.match checks if a string matches the regex from the beginning of the string, it does not search for the regex inside the string, so:

re.match("bc", "abc") # -> None

The function you probably had in mind is re.search:

re.search("bc", "abc") # -> <re.Match object; span=(1, 3), match='bc'>