开发者

Using regex, how to replace white space with no characters?

开发者 https://www.devze.com 2022-12-27 22:09 出处:网络
How to replace from regex many empty/blank characters with none? ex: <div class=\"someClass\" id=\"someID\">

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&开发者_如何学Cgt; 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消