Regex Replace [B]sometext[/B]

  • Thread starter Thread starter DDK
  • Start date Start date
D

DDK

I am trying to figure out how to Replace tags such as ... with the
correct HTML <b>...</b> tags in C#.

The code below works however only if one set of tags are found, if you have
more than two sets of the tags within the text it renders from the first [
and the last ].

Here is the code that doesn't quite work:
////////////////////////////////////////////////////////////////////////////
/////

// replace the bold special tags

text = Regex.Replace(text, "\\[B\\](?<boldText>.*)\\[/B\\]",

"<b>${boldText}</b>", RegexOptions.IgnoreCase);

////////////////////////////////////////////////////////////////////////////
//////

Any ideas on how to find and Replace multiple instances of tags such as
... in a paragraph of text?

Thanks,

d.
 
Can't see example data but I know from experience that there may not be
matched tags as your expression needs. If there is a mismatch then it will
perform as you are seeing.

Lloyd Sheen
 
try this,

text = Regex.Replace(text, "\\[B\\](?<boldText>.*?)\\[/B\\]",
"<b>${boldText}</b>", RegexOptions.IgnoreCase);

notice the ? after .*

it's called lazy or greedy wildcard
you can use google to get more info on that

or another way to solve this is just simply replace with <b> and
with </b> using 2 separate replace call

hope this help,

Du
 
little correction !!!
it's called lazy or greedy wildcard
it should be .. lazy or non-greedy

go here for more regex functions and examples
http://www.regular-expressions.info/reference.html
look under "quantifier"



Du Dang said:
try this,

text = Regex.Replace(text, "\\[B\\](?<boldText>.*?)\\[/B\\]",
"<b>${boldText}</b>", RegexOptions.IgnoreCase);

notice the ? after .*

it's called lazy or greedy wildcard
you can use google to get more info on that

or another way to solve this is just simply replace with <b> and
with </b> using 2 separate replace call

hope this help,

Du

DDK said:
I am trying to figure out how to Replace tags such as ... with the
correct HTML <b>...</b> tags in C#.

The code below works however only if one set of tags are found, if you have
more than two sets of the tags within the text it renders from the first [
and the last ].

Here is the code that doesn't quite work:
////////////////////////////////////////////////////////////////////////////
/////

// replace the bold special tags

text = Regex.Replace(text, "\\[B\\](?<boldText>.*)\\[/B\\]",

"<b>${boldText}</b>", RegexOptions.IgnoreCase);
////////////////////////////////////////////////////////////////////////////
//////

Any ideas on how to find and Replace multiple instances of tags such as
... in a paragraph of text?

Thanks,

d.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top