Regular Expressions

G

Guest

I have an XML in which I have to comment out the <responseopt> tag
the tags between this tag should not be commented

I plan to use regular expressions

The tags looks like this

<responseopt value="1"><someothertag></someothertag></responseopt>
<responseopt value="2"><someothertag></someothertag></responseopt>

Commenting the closing tag </responseopt> is easy. I just do a
replace(inputstring,"<responseopt>", "<!--<responseopt>-->"

how do I comment the opening tag.
So I need a regex feature which would search for a pattern beginging with
"<responseopt" and ending with ">" and I need to replace the ending part of
the string "-->" (the HTM comment closing)

Any suggestions????
 
G

Guest

The regular expression you are looking for is like this: \<(\/)?responseopt(
value=\"\d\")?\>

To test it in a javascript try this:

var str ='<responseopt value="1"><someothertag></someothertag></responseopt>';
var re =/\<(\/)?responseopt( value=\"\d\")?\>/g;
alert(str.replace(re,""));
 
G

Guest

And if you want to comment it out instead of replacing it with a blank, try
something like this:

var strInput ='<responseopt
value="1"><someothertag></someothertag></responseopt>';
var re1 =/\<responseopt/g;
var re2 =/\"\>/g;
var strOutput =
strInput.replace(re1,"<!--<responseopt").replace(re2,'">-->');
alert(strOutput);
 
G

Guest

And if you intend to run this server-side you might use named groups like this:

string strInput = @"<responseopt
value=""5""><someothertag></someothertag></responseopt>";
//With named groups, you can name individual matching groups and
//then refer to these groups within the expression programmatically.
string strOutput =Regex.Replace(strInput, @"\<(\/)?(?<tag>responseopt(
value=\""\d\"")?)\>", "<!--<${tag}>-->");
txtOutput.Text = strOutput;
 
G

Guest

Hey Phillip

This looks cool but I don't understand this and so I have a silly question.
The moment I changed my tag to have a space between word "responseopt" and
"value" it did not seem to comment the opening tag

So I need to comment this tag in mybig XML
<responseopt value="1">

Could you modify your named group for handling the space. Also yes, I have
to do this server side in vb.net and so I cannot use verbatim strings. So is
there a way out?

Appreciate.
P K
 
G

Guest

Put the space within the brackets that matches the attribute "value", like
this:( value=\""\d\"")?

I placed the complete code with detailed explanation and links to other
sites as a demo in VB at this link:
http://www.webswapp.com/CodeSamples/aspnet20/RegExp_NamedGroups.aspx

As for using verbatim strings in the demo; it should not be any different
when use a string that 50 times longer. It would work the same as long as
the pattern is consistent. If you still cannot do it using the provided
demo, post a real segment of the actual xml data and I will take a look at it.
 
G

Guest

Hey Thanks a ton.

I added a \s in the bracket. should have thought about this.
The link is good.
 
G

Guest

Your link looks great ..you missed out explaining the replace function and
the way you replace the tag for completness .
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top