Use Regex to remove Attributes

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top