Python – Limited Search
Python – Limited Search
Often, after receiving a search result, we need to search deeper within the existing search results. For example, given a body of text, we may want to retrieve a URL and extract its various components, such as the protocol and domain. In this case, we need to use the group function, which is used to separate the search results into different groups based on a given regular expression. We create these groups by using parentheses around the searchable portion to separate the main search results and excluding the fixed word 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 domain name: ",result.group(2)
print "The TLD: ",result.group(3)
When we run the above program, we get the following output –
The main web Address: https://www.tutorialspoint.com
The protocol: https
The domain name: www.tutorialspoint
The TLD: com