Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot understand the code for removing words with numbers [duplicate]

I want to remove words with numbers. After research I understood that

 s = "ABCD abcd AB55 55CD A55D 5555"
>>> re.sub("\S*\d\S*", "", s).strip()

This code works to solve my situation

However, I am not able to understand how this code works. I know about regex and I know individually \d recognizes all the numbers [0-9]. \S is for white spaces. and * is 0 or more occurrences of the pattern to its left

"\S*\d\S*"

This part I am not able to understand

But I am not sure I understand how this code identifies AB55.

Can anyone please explain to me? Thanks

like image 264
asspsss Avatar asked Jan 20 '26 06:01

asspsss


1 Answers

this replaces a digit with any non-space symbols around with empty string ""

the AB55 is viewed like : AB are \S*, 5 is \d, 5 is \S*

55CD : empty string is \S*, 5 is \d, 5CD is \S*

A55D : A is \S*, 5 is \d, 5D is \S*

5555 : empty string is \S*, 5 is \d, 555 is \S*

The re.sub("\S*\d\S*", "", s) replaces all this substrings to empty string "" and .strip() is useless since it removes whitespace at the begin and end of the previous result

like image 199
snamef Avatar answered Jan 23 '26 07:01

snamef



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!