Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Ternary inside a regex

by kiat (Vicar)
on Sep 23, 2005 at 12:02 UTC ( #494485=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

Is it possible to have a ternary expression within the replacement bit of the regular expression? I've the following code and I'm trying to fix the size of font (captured in $1) if it's greater than 5:

$message =~ s~\[size=(.+?)\](.+?)\[/size\]~'<font size="' . $1 . '">' + . "$2</font>"~eisg; # I'm trying to test $1 so that if it's greater than 5, # fix it at 5, else leave it unchanged.
Thanks in advance :)

Replies are listed 'Best First'.
Re: Ternary inside a regex
by izut (Chaplain) on Sep 23, 2005 at 12:17 UTC
    You can do that using the /e modifier:
    $a = "size=1111"; $a =~ s/(\d+)/($1 > 5) ? 5 : $1/e; print $a, "\n"

    Igor S. Lopes - izut
    surrender to perl. your code, your rules.
      Thanks, izut!

      Changed my code to the following and it works:

      $message =~ s~\[size=(.+?)\](.+?)\[/size\]~($1 > 5) ? "<font size=\"5\ +">$2</font>" : "<font size=\"$1\">$2</font>"~eisg;
        I think this way is easier to read:
        message =~ s~\[size=(.+?)\](.+?)\[/size\]~"<font size=\"".(($1 > 5) ? +5 : $1)."\>$2</font>"~eisg;

        Igor S. Lopes - izut
        surrender to perl. your code, your rules.
Re: Ternary inside a regex
by blazar (Canon) on Sep 23, 2005 at 15:04 UTC
    You're already using the /e modifier. Thus you can put into the replacement portion of the s/// operator "any" perl code. No reason why a ternary operator could not be there...
Re: Ternary inside a regex
by ww (Archbishop) on Sep 24, 2005 at 10:45 UTC
    At worst, another way with the ternary (or "conditional") operator:
    use vars qw ( $message); $message = qq ( Foo bar <font size="7"> phrase here </font> blivits. ) +; $message =~ s#(\d+)#($1 > 5) ? 5 : $1#eisg; if ( $1 ) { print "\n After s###, \$1 is: $1 \n ...and revised \$msg is: +$message \n"; } else { print "\n no \$1 found \n"; }
    and output is:
    After s###, $1 is: 7
    ...and revised $message is: Foo bar <font size="5"> phrase here </font> blivits.
    • the $message html fragment is well-formatted... for 4.0, but display elements are now better (well, that's what the experts say) done with css
    • the "sig" regex modifiers are not needed with this $message but might well be with more typical .html
    • and, while matching on <font size=" before the 7> is omitted only to keep the example simple (and is easy enough to add), this is a very limited and profoundly un-extensible method -- better to use an html parser, as suggested elsewhere. For example, a web'ster using <font... may well have multiple and/or variant forms of elements in a single font tag, as, for example,
      <font color="red" size=+1...>
    • and, finally, despite generous efforts by OP, I can't see how Re^2 or Re^3 can or do work ... even with "<"s replacing "["s would welcome education.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-03-01 16:59 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (13 votes). Check out past polls.