简体   繁体   中英

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". The output should be "Remove numbers in this string".

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. here are a couple examples. xx.xxxxxx.xxxx.xxxxxxxx, x.xx.xxxx.xxxxxxxx.xx.xxxxx, x.xx.xxxxxx, etc.

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.
  • Use \. to match a literal . character
  • Put \.\d+ in a group with () so you can quantify it to match any number of them.
  • Use re.sub() to replace it with an empty string to remove the match.
  • 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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