欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在?Python?中使用通配符匹配字符串的方法

 更新時間:2023年03月28日 10:22:38   作者:跡憶客  
這篇文章主要介紹了在?Python?中使用通配符匹配字符串的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

使用通配符匹配字符串:

  • 使用 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 結(jié)尾。

示例中的模式僅包含一個通配符,但您可以根據(jù)需要使用任意多個通配符。

請注意,星號 * 匹配所有內(nèi)容(一個或多個字符)。

如果要匹配任何單個字符,請將星號 * 替換為問號 ?

  • * 匹配所有內(nèi)容(一個或多個字符)
  • ? 匹配任何單個字符
  • [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 結(jié)尾。

fnmatch.fnmatch 方法接受一個字符串和一個模式作為參數(shù)。如果字符串與模式匹配,則該方法返回 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_ 開頭。

正則表達式中的 .* 字符用作匹配任何一個或多個字符的通配符。

  • . 匹配除換行符以外的任何字符。
  • 星號 * 與前面的正則表達式(點 .)匹配零次或多次。

我們使用反斜杠\字符來轉(zhuǎn)義點。 在擴展名中,因為正如我們之前看到的,點 . 在正則表達式中使用時具有特殊含義。換句話說,我們使用反斜杠來處理點。 作為文字字符。

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

列表推導用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。

在每次迭代中,我們使用 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']

. 匹配除換行符以外的任何字符。

通過使用點 . 在不轉(zhuǎn)義的情況下,正則表達式匹配任何以 2023_ 開頭,后跟任何單個字符并以 .txt 結(jié)尾的字符串。

如果大家在閱讀或編寫正則表達式時需要幫助,請參考我們的正則表達式教程

該頁面包含所有特殊字符的列表以及許多有用的示例。

如果想使用正則表達式檢查字符串是否與模式匹配,我們可以直接使用 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() 類將結(jié)果轉(zhuǎn)換為布爾值。

如果要對單個字符使用通配符,請刪除星號 * 。

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 值。

到此這篇關(guān)于在 Python 中使用通配符匹配字符串的文章就介紹到這了,更多相關(guān)Python通配符匹配字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論