Regular expression to match start of filename and filename extension

后端 未结 8 838
离开以前
离开以前 2020-12-08 15:35

What is the regular expression to match strings (in this case, file names) that start with \'Run\' and have a filename extension of \'.py\'?

The regular expression s

相关标签:
8条回答
  • 2020-12-08 16:20

    Warning:

    • jobscry's answer ("^Run.?.py$") is incorrect (will not match "Run123.py", for example).
    • orlandu63's answer ("/^Run[\w]*?.py$/") will not match "RunFoo.Bar.py".

    (I don't have enough reputation to comment, sorry.)

    0 讨论(0)
  • 2020-12-08 16:24
    /^Run.*\.py$/
    

    Or, in python specifically:

    import re
    re.match(r"^Run.*\.py$", stringtocheck)
    

    This will match "Runfoobar.py", but not "runfoobar.PY". To make it case insensitive, instead use:

    re.match(r"^Run.*\.py$", stringtocheck, re.I)
    
    0 讨论(0)
提交回复
热议问题