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

Useful Video Courses


Python Online Training

187 Lectures 17.5 hours

Malhar Lathkar


Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty


Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official


Python with Data Science

Best Seller

75 Lectures 13 hours

Eduonix Learning Solutions


Python 3 from scratch to become a developer in demand

Best Seller

70 Lectures 8.5 hours

Lets Kode It


Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

Abhilash Nelson