Python 3 - RegEx
Last Updated: 2021-11-19
Most Frequently Used: match, search and findall
re.match()
: only match at the beginning of the string, returns amatch
objectre.search()
: locate a match anywhere in string, returns amatch
objectre.findall()
: find all occurrences, returns a list of strings
>>> type(re.search("foo", "foobarfoo"))
<class '_sre.SRE_Match'>
>>> type(re.match("foo", "foobarfoo"))
<class '_sre.SRE_Match'>
Extract
re.match()/re.search()
re.match()
and re.search()
return a match
object:
>>> match = re.search("f(.*?),", "foo,faa,fuu,bar")
>>> match.groups()
('oo',)
match.group(0)
returns the string snippet that matches the pattern:
>>> match.group(0)
'foo,'
other group
captures the ones in ()
:
>>> match.group(1)
'oo'
re.findall()
re.findall()
returns a list, extract value using []
:
>>> match = re.findall("f(.*?),", "foo,faa,fuu,bar")
>>> match
['oo', 'aa', 'uu']
>>> match[0]
'oo'
Compiled Patterns
pattern = re.compile(pattern_string)
result = pattern.match(string)
is equivalent to
result = re.match(pattern_string, string)
re.compile()
returns a SRE_Pattern
object:
>>> type(re.compile("pattern"))
<class '_sre.SRE_Pattern'>
Notes
dot matches everything except newlines