Regular Expression?

T

TJ

Hi,

I want to know how Regular Expression can be used in this situation.
I want to replace some string in specific condition..
The condition is to replace string only if the string is NOT inside of tag.

For example,
<Test id='wow'>wow</Test>
I want to replace the wow with great...So output will be
<Test id='wow'>great</Test>

As you know, if I just simply use 'wow' regular expression, it won't work...

Anybody knows what regular expression am I supposed to use?


Thanks,
 
J

Jon Skeet [C# MVP]

TJ said:
I want to know how Regular Expression can be used in this situation.
I want to replace some string in specific condition..
The condition is to replace string only if the string is NOT inside of tag.

For example,
<Test id='wow'>wow</Test>
I want to replace the wow with great...So output will be
<Test id='wow'>great</Test>

As you know, if I just simply use 'wow' regular expression, it won't work...

Anybody knows what regular expression am I supposed to use?

Do you absolutely have to use a regular expression for this? Personally
I find that trying to work with XML using basic string handling is a
lot more error prone than using the XML APIs that are available. Is
there anything to stop you from iterating through all of the available
text nodes and just doing simple replacements within them?
 
T

TJ

Hi,

Thanks for your reply.
That's also one option I think.

However, actually, the input string is not XML.
I just gave the example for simplication.

Actual input string is HTML. It could be whole HTML, or part of HTML.
The thing I want is that I want to replace some string, if the string is NOT
inside of tag.

For example,

<Img src='wow.jpg'>wow</img>

The output would be <Img src='wow.jpg'>great</img>

If you have any other idea other than regular expression, please share it
with us.

Thanks,
 
J

Jeroen Mostert

TJ said:
Actual input string is HTML. It could be whole HTML, or part of HTML.
The thing I want is that I want to replace some string, if the string is NOT
inside of tag.
Regular expression examples for processing HTML are all over the web. Here's
the third hit I got from Google for "html regex tag", for example:
http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx

However, if you want to do it right and take into account things like
unclosed tags ("<br>"), mismatched tags and CDATA sections, a regex will not
help you (or else it'll be way too complicated to write); you'll need an
actual HTML parser. Google suggests there are multiple for .NET, some of
which might be useful.
 

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

Top