Regular Expression



Regular Expression

A regular expression specifies a set of strings that matches it.

Example

import re
pattern = "ca"
text = "caabscasca"
# compile
repatter = re.compile(pattern)
matchOB = repatter.match(text)
# no compile
# match First
matchOB = re.match(pattern, text)
if matchOB:
    print(matchOB.group())
# ca
# Search Anyware
matchOB = re.search(pattern, text)
if matchOB:
    print(matchOB.group())
    print(matchOB.start())
    print(matchOB.end())
    print(matchOB.span())
# ca
# 0
# 2
# (0, 2)
# findall
matchedList = re.findall(pattern, text)
if matchedList:
    print(matchedList)
# ['ca', 'ca', 'ca']
# finditer
iterator = re.finditer(pattern, text)
for match in iterator:
    print(match.group())
# ca
# 0
# 2
# (0, 2)
    print(match.start())

# ca
# 5
# 7
# (5, 7)
    print(match.end())

# ca
# 8
# 10
# (8, 10)
    print(match.span())