Python re group














































Python re group



The .group() method is used to group the first occurrence of a substring matching the specified pattern in the regex object. If there is a match , the  .group() method returns the first substring and NULL otherwise.

In this article , we will discuss the working of the .group() method.
  

Let us consider the following example.

CODE :

import re
reob = re.compile(r'\d\d\d\d\d\d') # matches 6 digit numbers
mo = reob.search("THE PINCODES ARE 470072 AND 470006")# two 6 digit numbers , 470072 and 470006
print("THE MATCHED PINCODE : "+mo.group())






OUTPUT :

THE MATCHED PINCODE : 470072




Since the method just captures the first occurrence of a pattern , only 470072 will be matched even though 470006 matches the regular expression pattern.



Comments