正则表达式匹配出现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