找到这样一行的正则表达式是什么:>

发布于 2024-09-07 03:26:45 字数 409 浏览 5 评论 0原文

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

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中的蝴蝶 2024-09-14 03:26:45

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

    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 中的 /> 之前的所有其他内容,并在替换中使用这些捕获的字符串。

参考文献

I don't think regex is the right tool for this job, but something like this will "work" some of the time.

    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>")
    );

The above Java snippet prints:

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

The regex is this (see also on rubular.com):

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

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

作死小能手 2024-09-14 03:26:45

您可以使用类似 #<([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.

尘世孤行 2024-09-14 03:26:45

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

<rect[^>]*/>

<circle[^>]*/>

如果您想匹配任何独立的标签,您应该查看 Crozins 解决方案

Like polygenelubricants noted I don't know what this would accomplish but this should be what you are looking for:

<rect[^>]*/>

and

<circle[^>]*/>

If you want to match any self-contained tag you should look at Crozins solution.

桃酥萝莉 2024-09-14 03:26:45

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文