Search code examples
pythonpython-re

Is it possible to only match wildcard in python regular expression?


It is the scenario:

Frontend pass the regular expression to backend, then backend get it and execute the regular expression. But i want to just execute the wildcard match.

For example: Frontend pass bash-1.*.0, it works well

But bash-1.(hello):*.0, it will not match bash-1.(hello):1.0

So is it possible only match wildcard by python re module?


Solution

  • You shoulde use the fnmatch module in the stdlib if you want glob-style matching. For example:

    import fnmatch
    print(fnmatch.fnmatch("bash-1.(hello):1.0","bash-1.(hello):*.0")) # True
    print(fnmatch.fnmatch("bash-1.(goodbye):1.0","bash-1.(hello):*.0")) # False