2

I get a string from XML likebelow:

This is the                                     Sample text I need to get       
all this               but only with single spaces

Actually I am using the following reg-ex which is converting matching pattern to white space but I need them to be stripped not replaced by white space.

The above string should be output:

This is the Sample text I need to get all this but only with single spaces

williamcarswell
  • 673
  • 2
  • 5
  • 16
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • You forgot to include the regex. Anyway, what do you mean by replacing white-space by white-space ? Is it: "I want to aggregate all white-space sequences to a single white-space" ? –  Oct 12 '11 at 10:05
  • Why not to use search? http://stackoverflow.com/questions/1898656/remove-whitespace-in-python-using-string-whitespace – Gandi Oct 12 '11 at 10:07

1 Answers1

9

Simple solution without regex:

s = 'This is the                                     Sample text I need to get       all this               but only with single spaces'
' '.join(s.split())
#'This is the Sample text I need to get all this but only with single spaces'

For completeness I'll add ekhumoro's comment to my answer:

It's worth pointing out that split() does more than simply splitting on runs of whitespace: it also strips any whitespace at the beginning and end of the input (which is usually what is wanted). It also correctly handles non-breaking spaces if the input is unicode (which will probably be relevant for XML).

naeg
  • 3,944
  • 3
  • 24
  • 29
  • 1
    But why not " ".join(s.split())? – Gandi Oct 12 '11 at 10:19
  • 1
    It's worth pointing out that `split()` does more than simply splitting on runs of whitespace: it also strips any whitespace at the beginning and end of the input (which is usually what is wanted). It also correctly handles non-breaking spaces if the input is unicode (which will probably be relevant for XML). – ekhumoro Oct 12 '11 at 13:38