找到这样一行的正则表达式是什么:>
我想要一个可用于查找以下几行的正则表达式:
<rect width='10px' height ='20px'/>
<rect width='20px' height ='22px'/>
<circle radius='20px' height ='22px'/>
并将它们替换为以下几行:
<rect width='10px' height ='20px'></rect>
<rect width='20px' height ='22px'></rect>
<circle radius='20px' height ='22px'></circle>
谢谢。
I want a regular expression that could be used to find the following lines:
<rect width='10px' height ='20px'/>
<rect width='20px' height ='22px'/>
<circle radius='20px' height ='22px'/>
and replace them by the these lines:
<rect width='10px' height ='20px'></rect>
<rect width='20px' height ='22px'></rect>
<circle radius='20px' height ='22px'></circle>
Thank you .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为正则表达式是完成这项工作的正确工具,但这样的东西有时会“起作用”。
上面的 Java 片段打印:
正则表达式是这样的(另请参阅 rubular.com):
本质上我们尝试捕获组 1 中的标签名称以及组 2 中的
/>
之前的所有其他内容,并在替换中使用这些捕获的字符串。参考文献
I don't think regex is the right tool for this job, but something like this will "work" some of the time.
The above Java snippet prints:
The regex is this (see also on rubular.com):
Essentially we try to capture what we hope is a tag name in group 1, and everything else until the
/>
in group 2, and use these captured strings in our substitution.References
您可以使用类似
#<([az]+)([^>]*)/>#
并替换为<$1$2>
。但正则表达式可能会有所不同,具体取决于您使用的正则表达式引擎。You could use something like this
#<([a-z]+)([^>]*)/>#
and replace with<$1$2></$1>
. But regexp might differ depending on what's regexp engine you're using.就像 Polygenelubricants 指出的那样,我不知道这会实现什么,但这应该是您正在寻找的:
。
如果您想匹配任何独立的标签,您应该查看 Crozins 解决方案
Like polygenelubricants noted I don't know what this would accomplish but this should be what you are looking for:
and
If you want to match any self-contained tag you should look at Crozins solution.
sed 's/<\([az]*\) \([^\/>]*\)\/>/<\1 \2><\/\1>/'
会做你想做的事(在本例中)
搜索模式:
<\([az]*\) \([^\/>]*\)\/>
替换模式:
<\1 \2><\/\1>
sed 's/<\([a-z]*\) \([^\/>]*\)\/>/<\1 \2><\/\1>/'
Would do what you want (in this case)
Search pattern:
<\([a-z]*\) \([^\/>]*\)\/>
Replace pattern:
<\1 \2><\/\1>