Korduste ehk matches otsing

Tärni või plussi kasutamine regulaaravaldistes

import re

text = "abc 123 def 1"

matches_plus = re.findall(r'\d+', text)
print(matches_plus)  # ['123', '1']

matches_star = re.findall(r'\d*', text)
print(matches_star)  # ['', '', '', '123', '', '', '', '', ‘1’, '']

NB! Tärn lisab lõppu ka tühja elemendi.

Väike spikker:

# \d tähendab numbrit, plussmärk tingib ühte või rohkem numbreid järjest, kuid tärnile sobib ka kui sisendis pole numbreid ja seetõttu tagastab iga elemendi järjendis tühjalt.

Loogeliste sulgudega kolme ‘o’ tähe otsing

import re

text = "hellooo helooo helo"

pattern = r'o{3}'
matches = re.findall(pattern, text)
print(matches)  # ['ooo', 'ooo']

Nurksulgudes kolme tähekorduse otsing

import re

text = "aaabbbcccddd"

pattern = r'[abc]{3}'
matches = re.findall(pattern, text)
print(matches)  # ['aaa', 'bbb', 'ccc']

Ümarsulgudega kolmekordse korduse otsing

import re

text = "hellohellohello"

pattern = r'(hello){3}'
matches = re.findall(pattern, text)
print(matches)  # [hellohellohello]