Home > Software engineering >  How to remove a digit pattern at the beginning of a string with regex
How to remove a digit pattern at the beginning of a string with regex

Time:07-28

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.

CodePudding user response:

  • 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)
  • Related