[Python] regex to match uuid4 not working with f-string

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    Working:

    #!/usr/bin/python3

    import re

    path = "/a/b/c/e72cc82c-e83a-431c-9f63-c8d80eec9307"

    if re.match(r"/a/b/c/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$", path):
    print("matched")
    else:
    print("didn't match anything")


    Failing:

    #!/usr/bin/python3

    import re

    PATH_PREFIX = "/a/b/c"
    path = "/a/b/c/e72cc82c-e83a-431c-9f63-c8d80eec9307"

    if re.match(rf"{PATH_PREFIX}/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$", path):
    print("matched")
    else:
    print("didn't match anything")


    I used the f-string in python regex a few years ago, and it was working. Not sure why it is failing now.

    Continue reading...

Compartilhe esta Página