简体   繁体   English

如何使用正则表达式删除字符串开头的数字模式

[英]How to remove a digit pattern at the beginning of a string with regex

I have a string like so, "123.234.567 Remove numbers in this string".我有一个这样的字符串,“123.234.567 删除此字符串中的数字”。 The output should be "Remove numbers in this string". output 应该是“删除此字符串中的数字”。

The digits in this string follow the pattern xx.xxx.xxxxx...(digits followed by a period), but the number of periods and digits between each period is not static.此字符串中的数字遵循模式 xx.xxx.xxxxx...(数字后跟句点),但句点和每个句点之间的数字不是 static。 here are a couple examples.这里有几个例子。 xx.xxxxxx.xxxx.xxxxxxxx, x.xx.xxxx.xxxxxxxx.xx.xxxxx, x.xx.xxxxxx, etc. xx.xxxxxx.xxxx.xxxxxxxx、x.xx.xxxx.xxxxxxxx.xx.xxxxx、x.xx.xxxxxx等

How can I remove these digits followed by periods in regex?如何删除这些数字后跟正则表达式中的句点?

So far I have something like this:到目前为止,我有这样的事情:

patt = re.compile('(\s*)[0-9].[0-9]*.[0-9]*(\s*)')

But this only works for a specific format.但这仅适用于特定格式。

  • Use ^ to match the beginning of the string.使用^匹配字符串的开头。
  • Use \d+ to match any number of digits.使用\d+匹配任意数量的数字。
  • Use \.使用\. to match a literal .匹配文字. character特点
  • Put \.\d+ in a group with () so you can quantify it to match any number of them.\.\d+()放在一个组中,以便您可以量化它以匹配任意数量的它们。
  • Use re.sub() to replace it with an empty string to remove the match.使用re.sub()将其替换为空字符串以删除匹配项。
  • Use a raw string so you can put literal backslashes in the regexp without having to escape them.使用原始字符串,这样您就可以在正则表达式中放置文字反斜杠,而不必转义它们。
patt = re.compile(r's*^\d+(?:\.\d+)+\s*')
string = patt.replace('', string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM