[英]Combined regex pattern to match beginning and end of string and remove a separator character
[英]How to remove a digit pattern at the beginning of a string with regex
我有一個這樣的字符串,“123.234.567 刪除此字符串中的數字”。 output 應該是“刪除此字符串中的數字”。
此字符串中的數字遵循模式 xx.xxx.xxxxx...(數字后跟句點),但句點和每個句點之間的數字不是 static。 這里有幾個例子。 xx.xxxxxx.xxxx.xxxxxxxx、x.xx.xxxx.xxxxxxxx.xx.xxxxx、x.xx.xxxxxx等
如何刪除這些數字后跟正則表達式中的句點?
到目前為止,我有這樣的事情:
patt = re.compile('(\s*)[0-9].[0-9]*.[0-9]*(\s*)')
但這僅適用於特定格式。
^
匹配字符串的開頭。\d+
匹配任意數量的數字。\.
匹配文字.
特點\.\d+
與()
放在一個組中,以便您可以量化它以匹配任意數量的它們。re.sub()
將其替換為空字符串以刪除匹配項。patt = re.compile(r's*^\d+(?:\.\d+)+\s*')
string = patt.replace('', string)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.