Python正则表达式,匹配字符串的绝对开头,之前没有别的

时间:2013-12-15 20:58:57

标签: python regex

我有以下正则表达式:

(\b)(con)    

匹配:

.con
con

但我只想匹配第二行'con'而不是'.con'。

这需要扩展以使我能够匹配替代词(CON | COM1 | LPT1)等。在这些场景中,我需要匹配后来的点和潜在的文件扩展名。我有这些正则表达式。我试图一次理解表达的一部分。

我怎样才能收紧我所要求的具体匹配?

2 个答案:

答案 0 :(得分:8)

修改

您可以使用非分隔的捕获组和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)添加到模式的开头,以便进行不区分大小写的匹配。

答案 1 :(得分:3)

^匹配字符串的开头:

^con

会起作用。