leo
leo

Reputation: 43

Regex replace a pattern with another pattern

Can anyone help me write a regex have below result to replace bbcode with html tag, for here i want to replace [b][/b] with <strong></strong>

So this:

 "fsdfs [b]abc[/b] dddfs [b]abc[/b] fdsfdsfs [b]abcfsdfs" 

Becomes:

 "fsdfs <strong>abc</strong> dddfs <strong>abc</strong> fdsfdsfs [b]abcfsdfs"

Would the following Regex help to solve this problem?

 string result = Regex.Replace(s, @"\[b\](.*?)\[\/b\]", @"\<stront\>(.*?)\<\/strong\>");

Upvotes: 2

Views: 701

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208665

The following should work:

string s = "fsdfs [b]abc[/b] dddfs [b]abc[/b] fdsfdsfs [b]abcfsdfs";
string result = Regex.Replace(s, @"\[b\](.*?)\[/b\]", @"<strong>$1</strong>");

Example: http://ideone.com/xwP1EL

Upvotes: 5

Related Questions