Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Regular expression

by sharief (Novice)
on Feb 03, 2012 at 12:28 UTC ( [id://951648]=perlquestion: print w/replies, xml ) Need Help??

sharief has asked for the wisdom of the Perl Monks concerning the following question:

HI monks, I want a regular expression to remove unwanted span tags like

This

<span> <span> <span style="color:#000000;"> <ix:nonNumeric name="uk-bus:PrincipalLocation-CityOr +Town">London</ix:nonNumeric> </span> </span> </span>

TO

 <ix:nonNumeric name="uk-bus:PrincipalLocation-CityOrTown">London</ix:nonNumeric>

I would like to remove the upper and lower span tags

Replies are listed 'Best First'.
Re: Regular expression
by marto (Cardinal) on Feb 03, 2012 at 12:38 UTC
Re: Regular expression
by chessgui (Scribe) on Feb 03, 2012 at 12:41 UTC
    my $input=<<HERE; <span> <span> <span style="color:#000000;"> <ix:nonNumeric name="uk-bus:PrincipalLocation-CityOr +Town">London</ix:nonNumeric> </span> </span> </span> HERE $input=~s/<\/?span[^>]*>//g; print $input;
Re: Regular expression
by repellent (Priest) on Feb 05, 2012 at 03:46 UTC
    useXML::Twig;
    XML::Twig->new( twig_handlers => { span => sub { $_->replace_with($_->children_copy) }, }, )->parse(<<'XML')->print; <d> <span> foobar <span> <baz>bax</baz> <span style="color:#000000;"> <ix:nonNumeric name="uk-bus:PrincipalLocation-CityOrTown">Lond +on</ix:nonNumeric> </span> </span> </span> </d> XML print "\n"; __END__ <d> foobar <baz>bax</baz><ix:nonNumeric name="uk-bus:PrincipalLocation-CityOr +Town">London</ix:nonNumeric></d>

      If the input is not well-formed XML but HTML, you can use parse_html instead of parse in order to internally convert the HTML to XHTML before processing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://951648]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found