1

我想要一个可用于查找以下行的正则表达式:

<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>

谢谢你 。

4

4 回答 4

1

就像 polygenelubricants 指出的那样,我不知道这会完成什么,但这应该是您正在寻找的:

<rect[^>]*/>

<circle[^>]*/>

如果您想匹配任何自包含标签,您应该查看 Crozins 解决方案。

于 2010-06-26T17:38:18.053 回答
1

您可以使用类似的东西#<([a-z]+)([^>]*)/>#并替换为<$1$2></$1>. 但是正则表达式可能会有所不同,具体取决于您使用的正则表达式引擎。

于 2010-06-26T17:40:44.403 回答
1

sed 's/<\([a-z]*\) \([^\/>]*\)\/>/<\1 \2><\/\1>/'

会做你想做的(在这种情况下)

搜索模式: <\([a-z]*\) \([^\/>]*\)\/>

替换模式: <\1 \2><\/\1>

于 2010-06-26T17:45:05.953 回答
1

我不认为正则表达式是这项工作的正确工具,但这样的东西有时会“工作”。

    String text =
        " <rect width='10px' height ='20px'/> \n" +
        " <rect width='20px' height ='22px'/> \n" +
        " <circle radius='20px' height ='22px'/> \n" +
        " <square/> <rectangle></rectangle> \n" +
        " <foo @!(*#&^#@/> <bar (!@*&(*@!#> </whatever>";
    System.out.println(
        text.replaceAll("<([a-z]+)([^>]*)/>", "<$1$2></$1>")
    );

上面的 Java 代码段打印:

 <rect width='10px' height ='20px'></rect> 
 <rect width='20px' height ='22px'></rect> 
 <circle radius='20px' height ='22px'></circle> 
 <square></square> <rectangle></rectangle> 
 <foo @!(*#&^#@></foo> <bar (!@*&(*@!#> </whatever>

正则表达式是这样的(另见 rubular.com):

/<([a-z]+)([^>]*)\/>/

本质上,我们尝试捕获我们希望是第 1 组中的标签名称,以及直到第/>2 组中的所有其他内容,并在我们的替换中使用这些捕获的字符串。

参考

于 2010-06-26T17:47:26.487 回答