我有以下正则表达式:
(\b)(con)
匹配:
.con con
但我只想匹配第二行'con'而不是'.con'.
这需要扩展以使我能够匹配替代词(CON | COM1 | LPT1)等.在这些场景中,我需要匹配后来的点和潜在的文件扩展名.我有这些正则表达式.我试图一次理解表达的一部分.
我怎样才能收紧我必须给出的具体比赛?
编辑:
您可以使用非分隔的捕获组和re.match
(它锚定到字符串的开头):
>>> from re import match >>> strs = ["CON.txt", "LPT1.png", "COM1.html", "CON.jpg"] >>> # This can be customized to what you want >>> # Right now, it is matching .jpg and .png files with the proper beginning >>> [x for x in strs if match("(?:CON|COM1|LPT1)\.(?:jpg|png)$", x)] ['LPT1.png', 'CON.jpg'] >>>
以下是正则表达式的细分:
(?:CON|COM1|LPT1) # CON, COM1, or LPT1 \. # A period (?:jpg|png) # jpg or png $ # The end of the string
您可能还希望添加(?i)
到模式的开头,以便进行不区分大小写的匹配.