简体   繁体   中英

Replace string with matching pattern using Regex

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.

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>

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.

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

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM