![]() |
|
[Tut] Python Regex – How to Count the Number of Matches? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Regex – How to Count the Number of Matches? (/thread-93808.html) |
[Tut] Python Regex – How to Count the Number of Matches? - xSicKxBot - 03-01-2020 Python Regex – How to Count the Number of Matches? I just wrote a regular expression in Python that matches multiple times in the text and wondered: how to count the number of matches? Consider the example where you match an arbitrary number of word characters Code: '[a-z]+'Code: 'python is the best programming language in the world'You can watch my explainer video as you read over the tutorial: How many matches are there in the string? To count the number of matches, you can use multiple methods: 1. Python re.findall() Use the re.findall(pattern, string) method that returns a list of matching substrings. Then count the length of the returned list. Here’s an example: Code: >>> import re
>>> pattern = '[a-z]+'
>>> text = 'python is the best programming language in the world'
>>> len(re.findall(pattern, text))
9Why is the result 9? Because there are nine matching substrings in the returned list of the Code: re.findall()Code: >>> re.findall(pattern, text)
['python', 'is', 'the', 'best', 'programming', 'language', 'in', 'the', 'world']This method works great if there are non-overlapping matches. 2. Python re.finditer() You can also count the number of times a given Code: patternCode: textCode: re.finditer(pattern, text)Specification: Code: re.finditer(pattern, text, flags=0)Definition: returns an iterator that goes over all non-overlapping matches of the Code: patternCode: textThe Code: flagsExample: You can use the iterator to count the number of matches. In contrast to the Code: re.findall()Code: import re
pattern = '[a-z]+'
text = 'python is the best programming language in the world'
for match in re.finditer(pattern, text): print(match) '''
<re.Match object; span=(0, 6), match='python'>
<re.Match object; span=(7, 9), match='is'>
<re.Match object; span=(10, 13), match='the'>
<re.Match object; span=(14, 18), match='best'>
<re.Match object; span=(19, 30), match='programming'>
<re.Match object; span=(31, 39), match='language'>
<re.Match object; span=(40, 42), match='in'>
<re.Match object; span=(43, 46), match='the'>
<re.Match object; span=(47, 52), match='world'> '''If you want to count the number of matches, you can use a simple Code: countCode: import re
pattern = '[a-z]+'
text = 'python is the best programming language in the world' count = 0
for match in re.finditer(pattern, text): count += 1 print(count)
# 9Or a more Pythonic solution: Code: import re
pattern = '[a-z]+'
text = 'python is the best programming language in the world' print(len([i for i in re.finditer(pattern, text)]))
# 9This method works great if there are non-overlapping matches. 3. Overlapping Matches The above two methods work great if there are no overlapping matches. If there are overlapping matches, the regex engine will just ignore them because it “consumes” the whole matching substrings and starts matching the next pattern only after the Code: stopSo if you need to find the number of overlapping matches, you need to use a different approach. The idea is to keep track of the start position in the previous match and increment it by one after each match: Code: import re
pattern = '99'
text = '999 ways of writing 99 - 99999' left = 0
count = 0
while True: match = re.search(pattern, text[left:]) if not match: break count += 1 left += match.start() + 1
print(count)
# 7By keeping track of the start index of the previous match in the left variable, we can control where to look for the next match in the string. Note that we use Python’s slicing operation Code: text[left:]Where to Go From Here You’ve learned three ways of finding the number of matches of a given pattern in a string. If you struggle with regular expressions, check out our free 20,000 word regex tutorial on the Finxter blog! It’ll give you regex superpowers! |