简体   繁体   中英

python fnmatch.fnmatch("[seq]") escape not working

According to the https://bugs.python.org/issue13929 , "[seq]" should be escaped by the backslash. However, when i run the same code, i have different result like bellow

在此处输入图像描述

I need to detect the string contains the "[" and "]". So, my solution is to change "[seq]" to the "{seq}" on both string and pattern like bellow.

string = string.replace("[", "{").replace("]", "}")
pattern = pattern.replace("[", "{").replace("]", "}")
fnmatch.fnmatch(string, pattern)

Could there be a better solution?

fnmatch does not support escaping with backslashes. Instead, you should enclose special characters in square brackets. From the docs :

For a literal match, wrap the meta-characters in brackets. For example, '[?]' matches the character '?'.

pattern = "*[[]Ver.2]*"

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