Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Regular expression

by sharief (Novice)
on Feb 02, 2012 at 13:51 UTC ( #951436=perlquestion: print w/ replies, xml ) Need Help??
sharief has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks i need help with regular expression:

I want to change span tags only if it has font-weight: bold; as :

example:

Like this

<span style="font-weight: bold;color:#000000;"> <span style="font-family:Arial MT;font-size: 25px; font-weight: bold;" +> <span style="color:#000000;" xmlns="http://www.w3.org/1999/xhtml">

TO

<span style="font-weight: bold;"> <span style="font-weight: bold;"> <span style="color:#000000;" xmlns="http://www.w3.org/1999/xhtml">
I used expression:
while($content =~ /<span style="(.*?)"/gi) { my $got=$1; if($got=~/font-weight: bold;/gi) { $content =~ s/<span style="(.*?)">/<span style="font-weight: bold;">/g +i; } }

There is no result for the above code

Any help for forming regular expression for this

Update: It seems the compiler goes to

Line 1:

<span style="font-weight: bold;color:#000000;">

It extracts style content using

while($content =~ /<span style="(.*?)"/gi)

Then i have assigned

$got=$1;

So i will get $1 = font-weight: bold;color:#000000;

Then i check whether it has font-weight: bold; using  if($got=~/font-weight: bold;/gi)

It has font-weight: bold; !!!

Now how will i move again to Line 1 to change it as

As per toolic $content =~ s/<span style="font.*/<span style="font-weight: bold;">/gi; this is not checking font-weight: bold;

Comment on Regular expression
Select or Download Code
Re: Regular expression
by roboticus (Canon) on Feb 02, 2012 at 14:12 UTC

    sharief:

    Print the matched value you found in $1 to the console, and it'll be immediately apparent why you don't find what you're looking for.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Regular expression
by toolic (Chancellor) on Feb 02, 2012 at 14:32 UTC
    I see no need for the while loop:
    use warnings; use strict; undef $/; my $content = <DATA>; $content =~ s/<span style="font.*/<span style="font-weight: bold;">/gi +; print "$content\n"; __DATA__ <span style="font-weight: bold;color:#000000;"> <span style="font-family:Arial MT;font-size: 25px; font-weight: bold;" +> <span style="color:#000000;" xmlns="http://www.w3.org/1999/xhtml">

    prints:

    <span style="font-weight: bold;"> <span style="font-weight: bold;"> <span style="color:#000000;" xmlns="http://www.w3.org/1999/xhtml">

    See also:

Re: Regular expression
by JavaFan (Canon) on Feb 02, 2012 at 14:42 UTC
    You ought to be using an HTML parser. But you may get away with:
    s/<span\s+style=\s*"[^"]*font-weight:\s*bold[^"]*"\s*>/<span style="fo +nt-weight: bold">/g;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-12-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls