1

Is there a way to use replace with a regex denoting any number of any white space (blank but also tab) with something? I am trying the following to contract any extension of multiple white space to just one but it doesn't work:

mystring.replace('\s+', ' ')
amphibient
  • 27,548
  • 48
  • 136
  • 231

5 Answers5

4

You cannot use a regular expression in the replace() method for strings, you have to use the re module:

import re
mystring = re.sub(r'\s+', ' ', mystring)

Note the r prefix before the string literal, this makes sure that the backslash in your regular expressions is interpreted properly. It wouldn't actually make a difference here, but for different escape sequences it can cause serious problems. For example '\b' is a backspace character but r'\b' is a backslash followed by a 'b', which is used for matching word boundaries in regex.

Andrew Clark
  • 192,132
  • 30
  • 260
  • 294
1

Try using re.sub:

import re
result = re.sub('\s+', ' ', mystring)
p.s.w.g
  • 141,205
  • 29
  • 278
  • 318
1

You can use str.split and str.join, to use regex you need re.sub :

>>> ' '.join('f  o  o\t\t bar'.split())
'f o o bar'
Ashwini Chaudhary
  • 232,417
  • 55
  • 437
  • 487
0

Try something like this

import re
re.sub('\s+',' ',mystring)
Alien11689
  • 441
  • 4
  • 5
0

Just like this.

import re
print re.sub(r'\s+', '_', 'hello there')
# => 'hello_there'
paulie.jvenuez
  • 295
  • 4
  • 11