正則表達式匹配出現0次或一次


There are two more repeating qualifiers. The question mark character, ?, matches either once or zero times;
you can think of it as marking something as being optional.
For example, home-?brew matches either 'homebrew' or 'home-brew'.

import re

str_list = ["u_ex200331.log", "u_ex200331_x.log", "u_ex200331_xxx.log", "u_ex203888_x_x.log"]

matchObj = [str for str in str_list if re.match('^(u_ex[0-9]{6}(_x)?\.log)$', str)]

print(matchObj)

結果輸出為:

['u_ex200331.log', 'u_ex200331_x.log']


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM