Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex pattern no working in Python but on RegExr?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading