關於python 的re.sub用法 | re.sub python用法
![關於python 的re.sub用法](https://i.imgur.com/axBPWDg.jpg)
importretext=“JGoodisahandsomeboy,heiscool,clever,andsoon…”print(re.sub(r’s+’,‘-’,text))JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on…print(re.sub(r’iss+’,‘-’,text))JGood-ahandsomeboy,he-cool,clever,andsoon…print(re.sub(r’s+.’,‘.’,text))JGoodisahandsomeboy,heiscool,clever,andsoon…text=“JGoodisahandsomeboy,heiscool,clever,andsoon…”print(re.sub(r’s+,s+’,‘,’,text))JGoodisahandsomeboy,heiscool,clever,andsoon…許多資料的介紹如下:re.sub re.sub用於替換字串中的匹配項。下面一個例子將字串...
![關於python 的re.sub用法](http://pix1.agoda.net/hotelimages/300/3008492/3008492_17121510550060424840.jpg?s=800x)
import retext = “JGood is a handsome boy, he is cool, clever, and so on…”print(re.sub(r’s+’, ‘-’, text))JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on…print(re.sub(r’iss+’, ‘-’, text))JGood -a handsome boy, he -cool, clever, and so on…print(re.sub(r’s+.’, ‘.’, text))JGood is a handsome boy, he is cool, clever, and so on…text = “JGood is a handsome boy , he is cool , clever , and so on…”print(re.sub(r’s+,s+’, ‘,’,text))JGood is a handsome boy,he is cool,clever,and so on…許多資料的介紹如下:
re.sub re.sub用於替換字串中的匹配項。下面一個例子將字串中的空格 ’ ’ 替換成 ‘-’ :
import re text = ”JGood is a handsome boy, he is cool, clever, and so on…” print re.sub(r‘s+’, ‘-‘, text)re.sub的函式原型為:re.sub(pattern, repl, string, count)
其中第二個函式是替換後的字串;本例中為’-‘
第四個引數指替換個數。預設為0,表示每個匹配項都替換。
re.sub還允許使用函式對匹配項的替換進行復雜的處理。如:re.sub(r’s’, lambda m: ‘[’ + m.group(0) + ‘]’, text, 0);將字串中的空格’ ‘替換為’[ ]’。
自己實驗了一下,結果的確把句子中的“ ”替換為“-”
text = “...
取得本站獨家住宿推薦 15%OFF 訂房優惠
本站住宿推薦 20%OFF 訂房優惠,親子優惠,住宿折扣,限時回饋,平日促銷
Python re.sub用法 | re.sub python用法
Python re.sub用法及代碼示例 | re.sub python用法
Python:正则表达式re.sub()替换功能 | re.sub python用法
Python的替换函数——strip() | re.sub python用法
re.sub正則替換詳解【轉】 | re.sub python用法
【整理】详解Python中re.sub | re.sub python用法
正则表达式 | re.sub python用法
關於python 的re.sub用法 | re.sub python用法
![](https://i.imgur.com/axBPWDg.jpg)