Python - Constrained Search


Many times, after we get the result of a search we need to search one level deeper into part of the existing search result. For example, in a given body of text we aim to get the web addresses and also extract the different parts of the web address like the protocol, domain name etc. In such scenario we need to take help of group function which is used to divide the search result into various groups bases on the regular expression assigned. We create such group expression by separating the main search result using parentheses around the searchable part excluding the fixed words we want match.

import re
text = "The web address is"

# Taking "://" and "." to separate the groups 
result ='([\w.-]+)://([\w.-]+)\.([\w.-]+)', text)
if result :
    print "The main web Address: ",
    print "The protocol: ",
    print "The doman name: ", 
    print "The TLD: ", 

When we run the above program, we get the following output −

The main web Address:
The protocol:  https
The doman name:  www.tutorialspoint
The TLD:  com