2

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
  • 29,770
  • 54
  • 146
  • 240

5 Answers5

5

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
  • 202,379
  • 35
  • 273
  • 306
1

Try using re.sub:

import re
result = re.sub('\s+', ' ', mystring)
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
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
  • 244,495
  • 58
  • 464
  • 504
0

Try something like this

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

Just like this.

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