首頁  >  文章  >  後端開發  >  我們如何在Python的正規表示式中找到每個匹配的精確位置?

我們如何在Python的正規表示式中找到每個匹配的精確位置?

王林
王林轉載
2023-08-31 12:13:34642瀏覽

我們如何在Python的正規表示式中找到每個匹配的精確位置?

介紹

re模組是我們在Python中使用的正規表示式。文字搜尋和更複雜的文字操作都使用正規表示式。像grep和sed這樣的工具,像vi和emacs這樣的文字編輯器,以及像Tcl、Perl和Python這樣的電腦語言都內建了正規表示式支援。

Python中的re模組提供了用於匹配正規表示式的函數。

定義我們要尋找或修改的文字的正規表示式稱為模式。文字字面量和元字元構成了這個字串。編譯函數用於建立模式。建議使用原始字串,因為正規表示式經常包含特殊字元。 (r字元用於指示原始字串。)這些字元在組合成模式之前不會被解釋。

可以使用其中一個函數將模式應用於文字字串,模式在組裝完成後使用。可用的函式包括Match、Search、Find和Finditer。

使用的語法

在這裡使用的正規表示式函數是:我們使用正規表示式函數來尋找匹配項。

re.match(): Determines if the RE matches at the beginning of the string. If zero or more characters at the beginning of the string match the regular expression pattern, the match method returns a match object.

p.finditer(): Finds all substrings where the RE matches and returns them as an iterator. An iterator delivering match objects across all non-overlapping matches for the pattern in a string is the result of the finditer method.

re.compile(): Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search(), and other methods described below. The expression’s behavior can be modified by specifying a flag's value. Values can be any of the following variables combined using bitwise OR (the | operator).

m.start(): m.start() returns the offset in the string at the match's start.

m.group(): You may use the multiple-assignment approach to assign each value to a different variable when mo.groups() returns a tuple of values, as in the areaCode, mainNumber = mo.groups() line below.

search: It is comparable to re.match() but does not require that we just look for matches at the beginning of the text. The search() function can locate a pattern in the string at any location, but it only returns the first instance of the pattern.

演算法

  • 使用import re導入正規表示式模組。

  • 使用re.compile()函數建立一個正規表示式物件。 (記得使用原始字串。)

  • 將要搜尋的字串傳遞給Regex物件的finditer()方法。這將會傳回一個Match物件。

  • 呼叫Match物件的group()方法傳回實際符合的文字字串。

  • 我們也可以使用span()方法在一個元組中取得起始和結束索引。

範例

 #importing re functions
import re
#compiling [A-Z0-9] and storing it in a variable p
p = re.compile("[A-Z0-9]")
#looping m times in p.finditer
for m in p.finditer('A5B6C7D8'):
#printing the m.start and m.group
   print m.start(), m.group()

輸出

這將產生輸出−

0 A
1 5
2 B
3 6
4 C
5 7
6 D
7 8

程式碼解釋

使用import re導入正規表示式模組。使用re.compile()函數建立一個正規表示式物件(“[A-Z0-9]”)並將其賦值給變數p。使用迴圈遍歷m,並將要搜尋的字串傳遞給正規表示式物件的finditer()方法。這將會傳回一個Match物件。呼叫Match物件的m.group()和m.start()方法以傳回實際匹配文字的字串。

範例

# Python program to illustrate
# Matching regex objects
# with groups
import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print(mo.groups())

輸出

這將產生輸出−

('415', '555-4242')

程式碼解釋

使用import re導入正規表示式模組。使用re.compile()函數建立一個正規表示式物件(r'(\d\d\d)-(\d\d\d-\d\d\d\d)'),並將其賦值給變數phoneNumRegex。將要搜尋的字串傳遞給Regex物件的search()方法,並將其儲存在變數mo中。這將會傳回一個Match物件。呼叫Match物件的mo.groups()方法以傳回實際匹配的文字字串。

結論

Python re模組提供的search()、match()和finditer()方法允許我們匹配正規表示式模式,並且如果匹配成功,它會提供Match物件實例。使用這個Match物件的start()、end()和span()方法來取得關於匹配字串的詳細資訊。

當有很多匹配項時,如果使用findall()將它們全部加載,您可能會面臨記憶體過載的風險。您可以透過使用finditer()方法來獲得所有潛在匹配項的迭代器對象,這將提高效率。

這表示finditer()提供了一個可調用對象,當呼叫時,將結果載入記憶體。

以上是我們如何在Python的正規表示式中找到每個匹配的精確位置?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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