How to work with Regex properly?

E

Evgeny Zoldin

Hi ALL.

I would like to strip out all comments from some HTML source code. E.g.,

html = "__block 1__<!--....-->__block 2__<!--...-->__block 3__"

then call

Regex.Replace(sString, "<!--[^>]*-->", "") returns

"__block 1__block 3__"

, but I want to receive

"__block 1____block 2____block 3__"

Surely, it's can be realized with string operations, but is it possible to
do it by the Regexp?

More commonly, in Regex language we can use that construction: [^(class of
symbols)], that means "match any 1 symbol excluding any of (class of
symbols)". How can man define the following "match any symbol-sequence that
differs from the given (string-longer-then-1-symbol)"??

Thanks
 
E

Eric Gunnerson [MS]

I think if you use:

"<!--.*?-->"

as your pattern, you'll get the behavior you want. The '?' makes *
non-greedy, so it matches the shortest possible string.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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