473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regex Replace [B]sometext[/B]

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.
Nov 16 '05 #1
3 2420
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

"DDK" <dd*******@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #2
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" <dd*******@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #3
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" <vi*******@hotmail.com> wrote in message
news:AC*********************@twister01.bloor.is.ne t.cable.rogers.com... 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" <dd*******@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike Gifford | last post by:
Hello, I've got a number of html fragments (in a template file) which are bundled together in a cms. I'm sure that there's a way through javascript to convert a piece of code like this: ...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
6
by: C# newbie | last post by:
Hello group, When I run an XPATH query first as: //*[contains(translate(.,\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\", \"abcdefghijklmnopqrstuvwxyz\")
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
6
by: Sačo Zagoranski | last post by:
Hi, could someone help me putting together a regex expression for my problem? I need my search filter to treat spaces and commas in the query the same way no matter how many there are... ...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.