I want to match all the files in a folder using regular expressions for some reason: I used this:
re.compile(r'\.*$')
But this is also matching hidden files and temp files. Is there a better option?
This makes the assumption that you're wanting to do something with these file names. As someone mentioned in the comments you should use glob. Since I'm not sure what you're going for with the 'temp' files this was the simplest thing. It will return no hidden files. Files is a list of file paths from your current working directory.
import os, glob
files = [f for f in glob.glob('./*') if os.path.isfile(f)]
Try re.compile(r'\w+\.*\w*') to match alphanumeric file names with a possible dot extension.
\w+ matches one or more alphanumeric file names [a-zA-Z0-9_]
\.* matches zero or more '.' characters
\w* matches zero or more file extension alphanumeric characters.
Kodos is an excellent Python regular expression developer/debugger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With