Regular expression to match start of filename and filename extension

后端 未结 8 888
离开以前
离开以前 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:12

    If you write a slightly more complex regular expression, you can get an extra feature: extract the bit between "Run" and ".py":

    >>> import re
    >>> regex = '^Run(?P.*)\.py$'
    >>> m = re.match(regex, 'RunFoo.py')
    >>> m.group('name')
    'Foo'
    

    (the extra bit is the parentheses and everything between them, except for '.*' which is as in Rob Howard's answer)

提交回复
热议问题