[Python] Can somebody explain a money regex that just checks if the value matches some pattern?

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    There are multiple posts on here that capture value, but I'm just looking to check to see if the value is something. More vaguely put; I'm looking to understand the difference between checking a value, and "capturing" a value. In the current case the value would be the following acceptable money formats:

    Here is a post that explains some about a money regex but I don't understand it a bit.

    .50
    50
    50.00
    50.0
    $5000.00
    $.50


    I don't want commas (people should know that's ridiculous).

    The thing I'm having trouble with are:

    1. Allowing for a $ at the starting of the value (but still optional)
    2. Allowing for only 1 decimal point (but not allowing it at the end)
    3. Understanding how it's working inside
    4. Also understanding out to get a normalized version (only digits and a the optional decimal point) out of it that strips the dollar sign.

    My current regex (which obviously doesn't work right) is:

    # I'm checking the Boolean of the following:
    re.compile(r'^[\$][\d\.]$').search(value)


    (Note: I'm working in Python)

    Continue reading...

Compartilhe esta Página