首頁  >  文章  >  後端開發  >  Python中如何使用通配符來匹配字串

Python中如何使用通配符來匹配字串

WBOY
WBOY轉載
2023-05-06 12:13:062004瀏覽

使用通配符來匹配字串:

  • 使用 fnmatch.filter() 方法從清單中取得符合模式的字串。

  • 使用 fnmatch.fnmatch() 方法檢查字串是否與模式相符。

import fnmatch

a_list = ['fql.txt', 'jiyik.txt', 'com.csv']

pattern = '*.txt'
filtered_list = fnmatch.filter(a_list, pattern)
print(filtered_list)  # ????️ ['fql.txt', 'jiyik.txt']

Python中如何使用通配符來匹配字串

如果我們更願意使用正規表示式,請向下捲動到下一個副標題。

fnmatch.filter 方法接受一個可迭代物件和一個模式,並傳回一個新列表,該列表僅包含與提供的模式相符的可迭代物件元素。

範例中的模式以任一或多個字元開頭,以 .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_jiyik.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 結尾。

fnmatch.fnmatch 方法接受一個字串和一個模式作為參數。如果字串與模式匹配,則該方法傳回 True,否則傳回 False。只需將星號 * 替換為問號 ? 如果您想匹配任何單一字元而不是任何一個或多個字元。

或者,我們可以使用正規表示式。

使用正規表示式使用通配符來匹配字串

使用通配符來匹配字串:

使用re.match() 方法檢查字串是否符合給定的模式。使用 .* 字元代替萬用字元。

import re

a_list = ['2023_fql.txt', '2023_jiyik.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_jiyik.txt']

re.compile 方法將正規表示式模式編譯成一個對象,該物件可用於使用其match()search() 方法進行比對。

這比直接使用 re.matchre.search 更有效,因為它保存並重複使用了正規表示式物件。

正規表示式以 2023_ 開頭。

正規表示式中的 .* 字元用作符合任何一個或多個字元的通配符。

  • . 符合換行符號以外的任何字元。

  • 星號 * 與前面的正規表示式(點 .)符合零次或多次。

我們使用反斜線\字元來轉義點。在副檔名中,因為如我們之前所看到的,點 . 在正規表示式中使用時具有特殊意義。換句話說,我們使用反斜線來處理點。作為文字字元。

我們使用列表理解來迭代字串列表。

清單推導用於對每個元素執行某些操作或選擇滿足條件的元素子集。

在每次迭代中,我們使用 re.match() 方法檢查目前字串是否與模式相符。

import re

a_list = ['2023_fql.txt', '2023_jiyik.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_jiyik.txt']

如果提供的正規表示式在字串中匹配,則 re.match 方法傳回一個匹配物件。

如果字串與正規表示式模式不匹配,則 match() 方法傳回 None

新清單僅包含原始清單中與模式相符的字串。

如果只想匹配任何單個字符,請刪除點後面的星號 *. 在正規表示式中。

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']

. 符合換行符號以外的任何字元。

透過使用點. 在不轉義的情況下,正規表示式符合任何以2023_ 開頭,後面跟著任何單一字元並以.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')

如果字符串与模式匹配,则 re.match() 方法将返回一个匹配对象,如果不匹配,则返回 None

我们使用 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 值。

以上是Python中如何使用通配符來匹配字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除