Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collapsing whitespace in a string

Tags:

python

regex

I have a string that kind of looks like this:

"stuff   .  // : /// more-stuff .. .. ...$%$% stuff -> DD"

and I want to strip off all punctuation, make everything uppercase and collapse all whitespace so that it looks like this:

"STUFF MORE STUFF STUFF DD"

Is this possible with one regex or do I need to combine more than two? This is what I have so far:

def normalize(string):
    import re

    string = string.upper()

    rex   = re.compile(r'\W')
    rex_s = re.compile(r'\s{2,}')

    result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
    result = rex.sub('', result) # this reduces all those spaces

    return result

The only thing that doesn't work is the whitespace collapsing. Any ideas?

like image 802
priestc Avatar asked Aug 13 '09 22:08

priestc


4 Answers

Here's a single-step approach (but the uppercasing actually uses a string method -- much simpler!):

rex = re.compile(r'\W+')
result = rex.sub(' ', strarg).upper()

where strarg is the string argument (don't use names that shadow builtins or standard library modules, please).

like image 199
Alex Martelli Avatar answered Oct 11 '22 23:10

Alex Martelli


s = "$$$aa1bb2 cc-dd ee_ff ggg."
re.sub(r'\W+', ' ', s).upper()
# ' AA1BB2 CC DD EE_FF GGG '

Is _ punctuation?

re.sub(r'[_\W]+', ' ', s).upper()
# ' AA1BB2 CC DD EE FF GGG '

Don't want the leading and trailing space?

re.sub(r'[_\W]+', ' ', s).strip().upper()
# 'AA1BB2 CC DD EE FF GGG'
like image 20
John Machin Avatar answered Oct 11 '22 21:10

John Machin


result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
result = rex.sub('', result) # this reduces all those spaces

Because you typo'd and forgot to use rex_s for the second call instead. Also, you need to substitute at least one space back in or you'll end up with any multiple-space gap becoming no gap at all, instead of a single-space gap.

result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
result = rex_s.sub(' ', result) # this reduces all those spaces
like image 42
Amber Avatar answered Oct 11 '22 21:10

Amber


Do you have to use regular expressions? Do you feel you must do it in one line?

>>> import string
>>> s = "stuff   .  // : /// more-stuff .. .. ...$%$% stuff -> DD"
>>> s2 = ''.join(c for c in s if c in string.letters + ' ')
>>> ' '.join(s2.split())
'stuff morestuff stuff DD'
like image 33
John Fouhy Avatar answered Oct 11 '22 23:10

John Fouhy