tags:

views:

38

answers:

1

How to replace from regex many empty/blank characters with none? ex:

<div class="someClass" id="someID"> 
...bunch of elements/content 
<input type="button" name="myInput" id="inputID" title="myInput Title" /> 
...bunch of elements/content 
</div> 

when replaced :

<a class="myselector" rel="I need this value"></a><div class="someClass" id="someID">...bunch of elements/content<input type="button" name="myInput" id="inputID" title="myInput Title" />...bunch of elements/content</div> 
+1  A: 

The expression \s+ will match one or more whitespace characters. Replace it with an empty string to remove them. E.g., in Python:

cleaned = re.sub(r'\s+', '', original)

If you plan to do this to HTML, you may damage it. At least replace with a single space instead:

cleaned = re.sub(r'\s+', ' ', original)

Or use a proper HTML manipulation library.

Max Shawabkeh
Note that `\s` does not just represent the space character but whitespace characters in general.
Gumbo
Won't this bork anything within a <pre> tag? (I know... you're just answering the question... and you did disclaim it with "use a proper HTML manipulation library")
Stephen