Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding a pattern in string python

Tags:

python

I have a very noisy string input and I am trying to clean it..

So, a part of the noisy string can be something like:

"big $price chair, 5x10"

Now removing symbols and other stuff are done! But I also want to remove

  5x10

for this I did this:

 def remove_numerics(self,string):
    return ' '.join([term for term in string.split() if not term[0].isdigit()])

Which solved this case

but if my string is:

    "big $price chair, x10"

Then it fails? what is a good pythonic way to solve this case also. many thanks.

like image 854
frazman Avatar asked Mar 14 '26 16:03

frazman


2 Answers

re.sub(r'\b[\dx]+\b', '', "big $price chair, 5x10")
like image 136
JBernardo Avatar answered Mar 17 '26 04:03

JBernardo


import re
new_string = re.sub(r', \d*x\d+', '', old_string)
like image 35
mayhewr Avatar answered Mar 17 '26 06:03

mayhewr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!