- 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 提要
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字潤色
- Python - 文字包裝
- Python - 頻度分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受限搜尋
Python - 搜尋和匹配
在使用正則表示式時,有兩個經常混淆的基本操作,但它們的顯著差異。re.match() 只檢查字串開頭的匹配項,而 re.search() 檢查字串中任何位置的匹配項。這在文字處理中扮演了重要角色,因為我們常常必須編寫正確的正則表示式,才能檢索出情感分析的文字塊,例如。
import re
if re.search("tor", "Tutorial"):
print "1. search result found anywhere in the string"
if re.match("Tut", "Tutorial"):
print "2. Match with beginning of string"
if not re.match("tor", "Tutorial"):
print "3. No match with match if not beginning"
# Search as Match
if not re.search("^tor", "Tutorial"):
print "4. search as match"
當我們執行以上程式時,我們獲得以下輸出 −
1. search result found anywhere in the string 2. Match with beginning of string 3. No match with match if not beginning 4. search as match
廣告