Use Regex to remove Attributes

S

shapper

Hello,

I have a string as follows:

<span class="A" id="A"><span class="B" id="B">Some Text</span></
span>

Basically I want to remove all the attributes from span B:

<span class="A" id="A"><span>Some Text</span></span>

So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:

<span class="A" id="A">...</span>

How can I do this? I think the best option would be Regex. Or not?

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
[...]
Basically I want to remove all the attributes from span B:

<span class="A" id="A"><span>Some Text</span></span>

So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:

<span class="A" id="A">...</span>

How can I do this? I think the best option would be Regex. Or not?

The best way to remove attributes in XML is to read the XML as XML, and
rewrite it as XML, without the attributes you want to remove.

..NET already has robust support for XML handling. Use that.

Pete
 
S

shapper

shapper said:
[...]
Basically I want to remove all the attributes from span B:
  <span class="A" id="A"><span>Some Text</span></span>
So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:
  <span class="A" id="A">...</span>
How can I do this? I think the best option would be Regex. Or not?

The best way to remove attributes in XML is to read the XML as XML, and
rewrite it as XML, without the attributes you want to remove.

.NET already has robust support for XML handling.  Use that.

Pete

Sorry, but what do your mean?
Being this a string wouldn't be easier to use a Regex.

I was able to isolate the part:
<span class="B" id="B">Some Text</span>

So now all I need is to remove class="B" id="B". Or maybe just get the
"Some Text":

String new = String.Format("<span>{0}</span>", Get Some Text);

Thanks,
Miguel
 
A

Arne Vajhøj

I have a string as follows:

<span class="A" id="A"><span class="B" id="B">Some Text</span></
span>

Basically I want to remove all the attributes from span B:

<span class="A" id="A"><span>Some Text</span></span>

So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:

<span class="A" id="A">...</span>

How can I do this? I think the best option would be Regex. Or not?

Maybe.

What is the selection criteria for where to remove?

If it is id="B" then you can use:

string s = @"<span class=""A"" id=""A""><span class=""B"" id=""B"">Some
Text</span></span>";
string s2 = Regex.Replace(s, @"<span[^>]+id=""B""[^>]*>", "<span>");

Arne
 
P

Peter Duniho

shapper said:
shapper said:
[...]
Basically I want to remove all the attributes from span B:
<span class="A" id="A"><span>Some Text</span></span>
So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:
<span class="A" id="A">...</span>
How can I do this? I think the best option would be Regex. Or not?
The best way to remove attributes in XML is to read the XML as XML, and
rewrite it as XML, without the attributes you want to remove.

.NET already has robust support for XML handling. Use that.

Sorry, but what do your mean?

What part of my reply don't you understand?
Being this a string wouldn't be easier to use a Regex.

Obviously, I disagree.

But Arne is always more than happy to produce regex expressions for
people (including me :) ), so if you really want to use regex, I'm sure
you can.

Pete
 
A

Arne Vajhøj

shapper said:
[...]
Basically I want to remove all the attributes from span B:

<span class="A" id="A"><span>Some Text</span></span>

So maybe the easiest way would be to get all the text inside the inner
span and then recreate everything inside:

<span class="A" id="A">...</span>

How can I do this? I think the best option would be Regex. Or not?

The best way to remove attributes in XML is to read the XML as XML, and
rewrite it as XML, without the attributes you want to remove.

.NET already has robust support for XML handling. Use that.

It looks as HTML/XHTML.

If it is in fact XHTML, then XML is a much more robust
solution than regex.

If it is not XHTML, then XML is not an option.

Arne
 

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