使用Regex替换匹配模式的字符串

时间:2021-06-13 16:50:18

I want to add a <span></span> to each of the tag in the following XML. I would like to use C# regular expression like this.

我想在以下XML中为每个标记添加 。我想像这样使用C#正则表达式。

Regex.Replace(xml, @"<*>", @"<span>" + @"<*>" + "</span>")

Original XML:

<div id="Content">
  <p>1</p>
  <h2>1</h2>
  <h2>2</h2>
</div>

Modified XML

<span><div id="Content"></span>
  <span><p></span>1<span></p></span>
  <span><h2></span>1<span></h2></span>
  <span><h2></span>2<span></h2></span>
<span></div></span>

3 个解决方案

#1


0  

Here is a working example of how to achieve this more or less safe:

这是一个如何实现这个或多或少安全的工作示例:

var xml = "<div id=\"Content\">\r\n  <p>1</p>\r\n  <h2>1</h2>\r\n  <h2>2</h2>\r\n</div>";
var result = Regex.Replace(xml, @"<[^>]+?>", @"<span>$&</span>");

The regex used is <[^>]+?> that just matches <, then anything that is not > up to >.

使用的正则表达式是<[^>] +?>,只匹配<,然后是任何不>>的。

Output:

使用Regex替换匹配模式的字符串

#2


1  

I suggest to avoid using regex with xhtml, since it's well known that there are better tools. You could use xml parser, xquery, xpath, etc.

我建议避免在xhtml中使用正则表达式,因为众所周知有更好的工具。您可以使用xml解析器,xquery,xpath等。

However, if you still have to use or want to use regex then you have to use capturing groups and also use a non greedy regex. You can use this:

但是,如果您仍然必须使用或想要使用正则表达式,那么您必须使用捕获组并使用非贪婪的正则表达式。你可以用这个:

(<.*?>)

working demo

#3


-1  

How about this

这个怎么样

            string input = "<div id=\"Content\">" +
                           "<p>1</p>" +
                           "<h2>1</h2>" +
                           "<h2>2</h2>" +
                           "</div>";
            string pattern = @"(</?\w+>)";

            string output = Regex.Replace(input, pattern, "<span>$1</span>");
            output = "<span>" + output + "</span>";​

#1


0  

Here is a working example of how to achieve this more or less safe:

这是一个如何实现这个或多或少安全的工作示例:

var xml = "<div id=\"Content\">\r\n  <p>1</p>\r\n  <h2>1</h2>\r\n  <h2>2</h2>\r\n</div>";
var result = Regex.Replace(xml, @"<[^>]+?>", @"<span>$&</span>");

The regex used is <[^>]+?> that just matches <, then anything that is not > up to >.

使用的正则表达式是<[^>] +?>,只匹配<,然后是任何不>>的。

Output:

使用Regex替换匹配模式的字符串

#2


1  

I suggest to avoid using regex with xhtml, since it's well known that there are better tools. You could use xml parser, xquery, xpath, etc.

我建议避免在xhtml中使用正则表达式,因为众所周知有更好的工具。您可以使用xml解析器,xquery,xpath等。

However, if you still have to use or want to use regex then you have to use capturing groups and also use a non greedy regex. You can use this:

但是,如果您仍然必须使用或想要使用正则表达式,那么您必须使用捕获组并使用非贪婪的正则表达式。你可以用这个:

(<.*?>)

working demo

#3


-1  

How about this

这个怎么样

            string input = "<div id=\"Content\">" +
                           "<p>1</p>" +
                           "<h2>1</h2>" +
                           "<h2>2</h2>" +
                           "</div>";
            string pattern = @"(</?\w+>)";

            string output = Regex.Replace(input, pattern, "<span>$1</span>");
            output = "<span>" + output + "</span>";​