在 Python 中使用通配符匹配字符串
使用通配符匹配字符串:
- 使用 fnmatch.filter() 方法从列表中获取匹配模式的字符串。
- 使用 fnmatch.fnmatch() 方法检查字符串是否与模式匹配。
import fnmatch
a_list = ['fql.txt', 'zadmei.txt', 'com.csv']
pattern = '*.txt'
filtered_list = fnmatch.filter(a_list, pattern)
print(filtered_list) # 👉️ ['fql.txt', 'zadmei.txt']
如果我们更愿意使用正则表达式,请向下滚动到下一个副标题。
示例中的模式以任意一个或多个字符开头,以 .txt 结尾。
如果要匹配任何单个字符,请将星号 * 替换为问号 ?。
- * 匹配所有内容(一个或多个字符)
- ? 匹配任何单个字符
- [sequence] 匹配序列中的任意字符
- [!sequence] 匹配任何不按顺序的字符
下面是使用问号匹配任何单个字符的示例。
import fnmatch
a_list = ['abc', 'abz', 'abxyz']
pattern = 'ab?'
filtered_list = fnmatch.filter(a_list, pattern)
print(filtered_list) # 👉️ ['abc', 'abz']
该模式匹配以 ab 开头后跟任何单个字符的字符串。
如果要使用通配符检查字符串是否与模式匹配,请使用 fnmatch.fnmatch() 方法。
import fnmatch
a_string = '2023_zadmei.txt'
pattern = '2023*.txt'
matches_pattern = fnmatch.fnmatch(a_string, pattern)
print(matches_pattern) # 👉️ True
if matches_pattern:
# 👇️ this runs
print('The string matches the pattern')
else:
print('The string does NOT match the pattern')
该模式以 2023 开头,后跟任意一个或多个字符,并以 .txt 结尾。
或者,我们可以使用正则表达式。
使用正则表达式使用通配符匹配字符串
使用通配符匹配字符串:
- 使用 re.match() 方法检查字符串是否匹配给定的模式。
- 使用 .* 字符代替通配符。
import re
a_list = ['2023_fql.txt', '2023_zadmei.txt', '2023_com.csv']
regex = re.compile(r'2023_.*\.txt')
list_of_matches = [
item for item in a_list
if re.match(regex, item)
]
print(list_of_matches) # 👉️ ['2023_fql.txt', '2023_zadmei.txt']
re.compile 方法将正则表达式模式编译成一个对象,该对象可用于使用其 match() 或 search() 方法进行匹配。
这比直接使用 re.match 或 re.search 更有效,因为它保存并重用了正则表达式对象。
正则表达式以 2023_ 开头。
- 点 . 匹配除换行符以外的任何字符。
- 星号 * 与前面的正则表达式(点 .)匹配零次或多次。
我们使用列表理解来迭代字符串列表。
列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们使用 re.match() 方法检查当前字符串是否与模式匹配。
import re
a_list = ['2023_fql.txt', '2023_zadmei.txt', '2023_com.csv']
regex = re.compile(r'2023_.*\.txt')
list_of_matches = [
item for item in a_list
if re.match(regex, item)
]
print(list_of_matches) # 👉️ ['2023_fql.txt', '2023_zadmei.txt']
如果提供的正则表达式在字符串中匹配,则 re.match 方法返回一个匹配对象。
新列表仅包含原始列表中与模式匹配的字符串。
如果只想匹配任何单个字符,请删除点后面的星号 *. 在正则表达式中。
import re
a_list = ['2023_a.txt', '2023_bcde.txt', '2023_z.txt']
regex = re.compile(r'2023_.\.txt')
list_of_matches = [
item for item in a_list
if re.match(regex, item)
]
print(list_of_matches) # 👉️ ['2023_a.txt', '2023_z.txt']
点 . 匹配除换行符以外的任何字符。
如果大家在阅读或编写正则表达式时需要帮助,请参考我们的正则表达式教程。
该页面包含所有特殊字符的列表以及许多有用的示例。
如果想使用正则表达式检查字符串是否与模式匹配,我们可以直接使用 re.match() 方法。
import re
a_string = '2023_fql.txt'
matches_pattern = bool(re.match(r'2023_.*\.txt', a_string))
print(matches_pattern) # 👉️ True
if matches_pattern:
# 👇️ this runs
print('The string matches the pattern')
else:
print('The string does NOT match the pattern')
我们使用 bool() 类将结果转换为布尔值。
如果要对单个字符使用通配符,请删除星号 * 。
import re
a_string = '2023_ABC.txt'
matches_pattern = bool(re.match(r'2023_.\.txt', a_string))
print(matches_pattern) # 👉️ False
if matches_pattern:
print('The string matches the pattern')
else:
# 👇️ this runs
print('The string does NOT match the pattern')
示例中的字符串与模式不匹配,因此 matches_pattern 变量存储一个 False 值。