except UserError
except UserError

Reputation: 191

Python match whole file name, not just extension

Objective: Use Python regex to return a list of filenames have certain file extensions.

List of files:

Regex to find files that end in *.ROM or 3x numbers:

bios_file = re.findall(r'.*\.(rom|[0-9]{3})+', name, re.I)

Issue: bios_file is returning ['ROM'], ['rom'], [928]. bios_file should be returning the entire filename ['X8SIA9.ROM'], etc. How can I have the full filename returned instead of just the file extension?

Upvotes: 0

Views: 85

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49320

You're not capturing the whole filename in the group. You can also use noncapturing groups with (?:...).

.*\.(rom|[0-9]{3})+ # from this
(.*\.(?:rom|[0-9]{3})) # to this

Upvotes: 3

Related Questions