CKTIS / Python
Regularni izrazi
Uzorak opisuje strukturu teksta, a fullmatch traži podudaranje cijelog niza.
Verzija: Python 3.14; repository image, production runtime not attested
Sintaksa
match = re.fullmatch(r'([A-Z]+)-([0-9]+)', text)Uzorak opisuje strukturu teksta, a fullmatch traži podudaranje cijelog niza.
search može pronaći podniz. Skupine hvataju tekst bez pretvorbe. [0-9] ograničava znamenke na ASCII, dok \d u uzorku str uobičajeno prepoznaje i druge decimalne znamenke Unicodea.
QA-07 daje ('QA', '07') sa sačuvanom nulom. Prefiks i znamenka pune širine daju None.
import re
p=re.compile(r'(?P<team>[A-Z]+)-(?P<number>[0-9]+)')
m=p.search('id QA-07!')
assert m.group('number')=='07' and m.span()==(3,8)
assert p.fullmatch('id QA-07!') is None
assert re.fullmatch(r'\d','7') and not re.fullmatch(r'[0-9]','7')
Primjeri
import re
pattern = re.compile(r"(?P<team>[A-Z]+)-(?P<number>[0-9]+)")
for text in ["QA-07", "prefix QA-07", "QA-7"]:
match = pattern.fullmatch(text)
print(match.groups() if match else None)
Rezultat
('QA', '07')
None
NoneQA-07 daje ('QA', '07') sa sačuvanom nulom. Prefiks i znamenka pune širine daju None.