如何从正则表达式替换许多空/空白字符?例如:
<div class="someClass" id="someID">
...bunch of elements/content
<input type="button" name="myInput" id="inputID" title="myInput Title" />
...bunch of elements/content
</div>
更换时:
<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>
答案 0 :(得分:2)
表达式\s+
将匹配一个或多个空白字符。将其替换为空字符串以将其删除。例如,在Python中:
cleaned = re.sub(r'\s+', '', original)
如果您计划对HTML执行此操作,则可能会损坏它。至少替换为单个空格:
cleaned = re.sub(r'\s+', ' ', original)
或者使用适当的HTML操作库。