Regular expression to match start of filename and filename extension

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 should match any of the following:

It should not match:

The SQL equivalent of what I am looking for is ... LIKE '' ....

Asked by: Kelvin855 | Posted: 01-10-2021

Answer 1

For a regular expression, you would use:


A quick explanation:

  • . means match any character.
  • * means match any repetition of the previous character (hence .* means any sequence of chars)
  • \ is an escape to escape the explicit dot
  • $ indicates "end of the string", so we don't match ""

However, for this task, you're probably better off using simple string methods. ie.

filename.startswith("Run") and filename.endswith(".py")

Note: if you want case insensitivity (ie. matching "run.PY" as well as "", use the re.I option to the regular expression, or convert to a specific case (eg filename.lower()) before using string methods.

Answered by: Arnold927 | Posted: 02-11-2021

Answer 2

I don't really understand why you're after a regular expression to solve this 'problem'. You're just after a way to find all .py files that start with 'Run'. So this is a simple solution that will work, without resorting to compiling an running a regular expression:

import os
for filename in os.listdir(dirname):
    root, ext = os.path.splitext(filename)
    if root.startswith('Run') and ext == '.py':
        print filename

Answered by: Tara160 | Posted: 02-11-2021

Answer 3


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

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

Answered by: Kevin363 | Posted: 02-11-2021

Answer 4


Or, in python specifically:

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

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

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

Answered by: Dexter227 | Posted: 02-11-2021

Answer 5

You don't need a regular expression, you can use glob, which takes wildcards e.g. Run*.py

For example, to get those files in your current directory...

import os, glob
files = glob.glob( "".join([ os.getcwd(), "\\Run*.py"]) )

Answered by: Brooke806 | Posted: 02-11-2021

Answer 6

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<name>.*)\.py$'
>>> m = re.match(regex, '')

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

Answered by: David784 | Posted: 02-11-2021

Answer 7

This probably doesn't fully comply with file-naming standards, but here it goes:


Answered by: Sawyer473 | Posted: 02-11-2021

Answer 8



just a quick try

Answered by: Brianna932 | Posted: 02-11-2021

Similar questions

regex - Python regular expression match with file extension

I want to use Python regular expression utility to find the files which has this pattern: 000014_L_20111026T194932_1.txt 000014_L_20111026T194937_2.txt ... 000014_L_20111026T194928_12.txt So the files I want have an underscore '_' followed by a number (1 or more digits) and then followed by '.txt' as the extension. I used the following regular expression but it didn't match the above names...

Open expression in Python for a text file with unknown extension but same layout of the data inside of it

I am writing a script in python that will read in data from a text file and perform various operations on the data. It is working perfectly if I specify the extension of my file in this statement with open("file.txt") as f:. My question is (and I don't even know if this is possible), I have to open a file that someone will provide to me but I let's assume that they will all provide it with different extensions...

Apply asterisk extension expression matching in a python script

I would like to emulate asterisk extension matching in python. For example: 3494ZXXXXXX Would match: 34941000000 But not match: 34940000000 I would like to support any matching operator provided by asterisk. I do not have an asterisk available, so no AMI can be used. So what I need is a way to implement this function: ...

python - How to check with Regular Expression if user's input is valid file's extension?

Closed. This question needs debugging detai...

python - I want to split file’s extension by using regular expression

item is a string like "./test/test1.csv" . I want to change item into "test1". I wrote code,'./*.csv',item) But,"1.csv" is item.I really cannot understand why such a thing happens.What should I do to do my ideal thing?

regex - Regular expression in python to get the last occurence of a file extension in a URL or path

Given a long url or path how do I get the last file extension in it. For example consider these two strings. url = '' path = './' The last extension is jpg and comes after the last . and before the followin...

regex - python regular expression to split paragraphs

How would one write a regular expression to use in python to split paragraphs? A paragraph is defined by 2 linebreaks (\n). But one can have any amount of spaces/tabs together with the line breaks, and it still should be considered as a paragraph. I am using python so the solution can use python's regular expression syntax whi...

python - Problem with Boolean Expression with a string value from a lIst

I have the following problem: # line is a line from a file that contains ["baa","beee","0"] line = TcsLine.split(",") NumPFCs = eval(line[2]) if NumPFCs==0: print line I want to print all the lines from the file if the second position of the list has a value == 0. I print the lines but after that the following happens: Traceback (most recent call last): ['baaa'...

python - split twice in the same expression?

Imagine I have the following: inFile = "/adda/adas/sdas/hello.txt" # that instruction give me hello.txt Name ="/") [-1] # that one give me the name I want - just hello Name1 = Name.split(".") [0] Is there any chance to simplify that doing the same job in just one expression?

python - Regular expression to extract URL from an HTML link

I’m a newbie in Python. I’m learning regexes, but I need help here. Here comes the HTML source: &lt;a href="" target="_blank"&gt;;/a&gt; I’m trying to code a tool that only prints out Can you help me please?

regex - How can I translate the following filename to a regular expression in Python?

I am battling regular expressions now as I type. I would like to determine a pattern for the following example file: b410cv11_test.ext. I want to be able to do a search for files that match the pattern of the example file aforementioned. Where do I start (so lost and confused) and what is the best way of arriving at a solution that best matches the file pattern? Thanks in advance.

python - What is the regular expression for the "root" of a website in django?

I'm using django and when users go to I want to point them to the index view. Right now I'm doing this: (r'^$', 'ideas.idea.views.index'), However, it's not working. I'm assuming my regular expression is wrong. Can anyone help me out? I've looked at python regular expressions but they didn't help me.

regex - Python Regular Expression to add links to urls

I'm trying to make a regular expression that will correctly capture URLs, including ones that are wrapped in parenthesis as in ( and spoken about on coding horror at I'm currently using the foll...

python - Regular expression to detect semi-colon terminated C++ for & while loops

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this: for (int i = 0; i &lt; 10; i++); ... but not this: for (int i = 0; i &lt; 10; i++) This looks trivial at first glance, until you realise...

regex - How do i write a regular expression for the following pattern in python?

How do i look for the following pattern using regular expression in python? for the two cases Am looking for str2 after the "=" sign Case 1: str1=str2 Case 2: str1 = str2 please note there can be a space or none between the either side of the "=" sign Mine is like this, but only works for one of the cases! m=re...

regex - Why is the regular expression returning an error in python?

Am trying the following regular expression in python but it returns an error import re ... #read a line from a file to variable line # loking for the pattern 'WORD' in the line ...'(?&lt;=[WORD])\w+',str(line)) i get the following error: AttributeError: 'NoneType' object has no attribute 'group'

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python