简体   繁体   中英

regular expressions in python for searching file

I want to know how to get files which match this type: recording_i.file_extension
Ex:

recording_1.mp4
recording_112.mp4
recording_11.mov

I have a regular expression:

(recording_\d*)(\..*)

My regular expression doesn't works as i want.

Wrong file names which not match my type: lalala_recording_1.mp4, recording_.mp4 But my re works for this examples, however my code should return [] for this examples. Can u fix my regular expression, please? Thanks.

Use

(^recording_\d+)(\.\w{3}$)

Test

import re

s = """recording_1.mp4
recording_112.mp4
recording_11.mov
lalala_recording_1.mp4, 
recording_.mp4"""

pattern = re.compile(r"(^recording_\d+)(\.\w{3}$)")

for l in s.split():
  if pattern.match(l):
    print(l)

Output (only the desired files)

recording_1.mp4
recording_112.mp4
recording_11.mov

Explanation

With r"(^recording_\d+)(\.\w{3}$)"--1) 
- use \d+ since need at least one number
- \w{3} for three letter suffix
- ^ to ensure starts with recording
- $ to ensure ends after suffix

Particular Suffixes

import re

# List of suffixes to match
suffixes_list = ['mp4', 'mov']
suffixes = '|'.join(suffixes_list)
# Use suffixes in pattern (rather than excepting
# any 3 letter word
pattern = re.compile(fr"(^recording_\d+)(\.{suffixes}$)")

Test

s = """recording_1.mp4
recording_112.mp4
recording_11.mov
lalala_recording_1.mp4, 
recording_.mp4
dummy1.exe
dummy2.pdf
dummy3.exe"""

for l in s.split():
  if pattern.match(l):
    print(l)

Output

recording_1.mp4
recording_112.mp4
recording_11.mov

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM