Python Constrained Search
Python Constrained Search
Many times, after obtaining search results, we need to perform further searches within the existing results. For example, given text, our goal is to retrieve URLs and extract their various components, such as the protocol and domain. In this case, we need to use the group function to group the search results into groups based on a specified regular expression. We create such group expressions by using parentheses around the searchable parts and excluding the fixed words we want to match.
import re
text = "The web address is https://www.tutorialspoint.com"
# Taking "://" and "." to separate the groups
result = re.search('([w.-]+)://([w.-]+).([w.-]+)', text)
if result :
print "The main web Address: ",result.group()
print "The protocol: ",result.group(1)
print "The doman name: ",result.group(2)
print "The TLD: ",result.group(3)
When we run the above program, we will get the following output −
The main web Address: https://www.tutorialspoint.com
The protocol: https
The doman name: www.tutorialspoint
The TLD: com