2

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\>");
4
  • <stront\> should probably be <strong\> what ever you try to do. Commented Apr 10, 2013 at 23:19
  • I understood it, the question was essentially: how can I convert "fsdfs [b]abc[/b] dddfs [b]abc[/b] fdsfdsfs [b]abcfsdfs" to "fsdfs <strong>abc</strong> dddfs <strong>abc</strong> fdsfdsfs [b]abcfsdfs" with regex. Putting both the input and output on the same line was an unfortunate choice. Commented Apr 10, 2013 at 23:21
  • I'm guessing that you only want to replace [B] with [STRONG] where the [B] has a closing [/B] in the original string. This is not a good thing to do in Regex, because regex is not good for parsing HTML. Why? Read this: stackoverflow.com/questions/1732348/… Commented Apr 10, 2013 at 23:21
  • Why are you escaping the angle brackets in the replacement? Commented Apr 10, 2013 at 23:24

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

As a thought experiment, this works very well. In practice though, it's not a good way to manage Html at all; It can't hurt to consider using something like the Html Agility pack instead. stackoverflow.com/questions/6540154/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.