![](/img/trans.png)
[英]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.