- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不變性
- Python - 排序行
- Python - 重新設定段落格式
- Python - 統計段落中的標記
- Python - ASCII 二進位制轉換
- Python - 字串作為檔案
- Python - 向後讀取檔案
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取 URL
- Python - 漂亮列印
- Python - 文字處理狀態機
- Python - 大寫字母和小寫字母的轉換
- Python - 詞彙化
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 詞彙替換
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 詞彙標記
- Python - 塊和斷塊
- Python - 塊分類
- Python - 文字分類
- Python - 雙元組
- Python - 處理 PDF
- Python - 處理 Word 文件
- Python - 讀取 RSS feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字詞法轉換
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹演算法
- Python - 受約束搜尋
Python - 受約束搜尋
搜尋結果出來後,我們經常需要對現有搜尋結果的某一部分進行更深層次的搜尋。例如,在給定的文字主體中,我們的目標是獲取網址,並提取網址的不同部分(如協議、域名等)。在這種情況下,我們需要藉助分組函式,該函式可根據分配的正則表示式將搜尋結果劃分為不同的組。我們透過使用圓括號將可搜尋部分與需要匹配的固定單詞分離開來,建立此類組表示式。
import re
text = "The web address is https://tutorialspoint.tw"
# 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://tutorialspoint.tw The protocol: https The doman name: www.tutorialspoint The TLD: com
廣告