【发布时间】:2016-11-26 00:15:15
【问题描述】:
对于以下输入
I/O 1< img > '< input >
I/O 1<' img > '< input >
我想要如下所需的输出,如果存在< 后跟空格,则应该会发生这种情况。
I/O 1<img>'<input>
谁能帮我处理正则表达式?
【问题讨论】:
对于以下输入
I/O 1< img > '< input >
I/O 1<' img > '< input >
我想要如下所需的输出,如果存在< 后跟空格,则应该会发生这种情况。
I/O 1<img>'<input>
谁能帮我处理正则表达式?
【问题讨论】:
s= "I/O 1< img > '< input >"
使用 s.find('
s[0 : s.find('
s[s.find('
s.replace(' ','') 将用 no_spaces 替换空格
( s[0:s.find('<')] ) + ( s[s.find('<'):].replace(' ','') )
【讨论】:
试试<\s+、\s+>和>\s+:
import re
s = "I/O 1< img > '< input >"
s = re.sub(r"<\s+", "<", s)
s = re.sub(r"\s+>", ">", s)
s = re.sub(r">\s+", ">", s)
print(s)
输出:
I/O 1<img>'<input>
【讨论】: