tags:

views:

6884

answers:

4

I have a string of test like this:

<customtag>hey</customtag>

I want to use a RegEx to modify the text between the "customtag" tags so that it might look like this:

<customtag>hey, this is changed!</customtag>

I know that I can use a MatchEvaluator to modify the text, but I'm unsure of the proper RegEx syntax to use. Any help would be much appreciated.

+5  A: 

I'd chew my own leg off before using a regular expression to parse and alter HTML.

Use XSL or DOM.


Two comments have asked me to clarify. The regular expression substitution works in the specific case in the OP's question, but in general regular expressions are not a good solution. Regular expressions can match regular languages, i.e. a sequence of input which can be accepted by a finite state machine. HTML can contain nested tags to any arbitrary depth, so it's not a regular language.

What does this have to do with the question? Using a regular expression for the OP's question as it is written works, but what if the content between the <customtag> tags contains other tags? What if a literal < character occurs in the text? It has been 11 months since Jon Tackabury asked the question, and I'd guess that in that time, the complexity of his problem may have increased.

Regular expressions are great tools and I do use them all the time. But using them in lieu of a real parser for input that needs one is going to work in only very simple cases. It's practically inevitable that these cases grow beyond what regular expressions can handle. When that happens, you'll be tempted to write a more complex regular expression, but these quickly become very laborious to develop and debug. Be ready to scrap the regular expression solution when the parsing requirements expand.

XSL and DOM are two standard technologies designed to work with XML or XHTML markup. Both technologies know how to parse structured markup files, keep track of nested tags, and allow you to transform tags attributes or content.

Here are a couple of articles on how to use XSL with C#:

Here are a couple of articles on how to use DOM with C#:

Here's a .NET library that assists DOM and XSL operations on HTML:

Bill Karwin
Well, I use them occasionally, on controlled environments, with machine generated code that is known to be consistent, for a quick job...
PhiLho
Then why don't you show us how to do it with XSL or DOM in C#? It's easy to make sweeping statements. Let's see the actual code. Regexes are not suitable for parsing general HTML, but they're perfectly suitable for doing specific things with specific HTML code.
Jan Goyvaerts
I admit you made me laugh, but let's have an explanation, or a link to a good explanation of why you'd rather chew your leg off. I guess it's really obvious why to some programmers, but maybe not to the novice?
Sam Murray-Sutton
+6  A: 

I wouldn't use regex either for this, but if you must this expression should work: <customtag>(.+?)</customtag>

Tjofras
Thanks - this worked perfectly. Normally I wouldn't use RegEx to parse HTML like this, but this HTML is from an internal system and is properly formed.
Jon Tackabury
As a warning to others: it wouldn't work on the properly formed syntax |<customtag><customtag>Some text</customtag>|</customtag> - the area between the pipe symbols is matched, so the second <customtag> would be deleted leaving badly formed XML.
Tom Leys
Yea and this is why you should not try to parse xml with regex. You could limit what can go inbetween the tags and just allow letter, numbers and spaces and it would work a little better. But then its restricted to a specific domain, so something like this: <customtag>([a-zA-Z0-9 ])+</customtag>
Tjofras
Or just <customtag>([^<]+)</customtag>. But yes HTML is not a regular language, so in the more general case you can't use regular expressions to match it. It's the same problem as using a regexp to match balanced parentheses.
Bill Karwin
A: 

If there won't be any other tags between the two tags, this regex is a little safer, and more efficient:

<customtag>[^<>]*</customtag>
Jan Goyvaerts
A: 

//This is to replace all HTML Text

var re = new RegExp("<[^>]*>", "g");

var x2 = Content.replace(re,"");

//This is to replace all  

var x3 = x2.replace(/\u00a0/g,'');;

sajoshi