pythonregexpython-re

Why do certain regex functions return a match object and a few don't?


In Python common regex functions, re.match, re.search, re.fullmatch, etc. return a match object and to print the result we have to use match.group():

re.search(pattern, string): Searches for the first occurrence of the pattern in the string and returns a match object.

match = re.search(r'\d+', 'There are 123 apples')
if match:
    print(match.group())  # Output: '123'

But results for regex functions like re.findall, re.split, re.sub, etc. can be printed directly:

re.split(pattern, string): Splits the string by the occurrences of the pattern and returns a list.

result = re.split(r'\s+', 'Split this string by spaces')
print(result)  # Output: ['Split', 'this', 'string', 'by', 'spaces']

Solution

  • These groups of functions just have different purposes. Those like re.match, re.search, re.fullmatch just tell you if there is a match in the text and where that match is:

    re.search(r'\d+', 'There are 123 apples')
    

    result is: <re.Match object; span=(10, 13), match='123'>

    re.match(r'There', 'There are 123 apples')
    

    result is: <re.Match object; span=(0, 5), match='There'>

    So you use these functions when you want to find the first place in your string where the pattern can be found or just to confirm whether the pattern is in your string. You don't usually want to print the pattern or use it as a variable.

    While the point of re.split, re.findall etc is to give you a list of strings. that you either need to print or to use further in the script.

    re.split(r'\s+', 'Split this string by spaces')
    

    ['Split', 'this', 'string', 'by', 'spaces']

    re.findall("o[a-z]", 'Find groups of o+ other letters')
    

    ['ou', 'of', 'ot']

    Hope this clears it.