Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Can this be a 1 line or simpler regex?

by smackdab (Pilgrim)
on Apr 05, 2003 at 05:52 UTC ( [id://248262]=perlquestion: print w/replies, xml ) Need Help??

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

$_ = "one\ttwo\nthree four five\r"; s/\t/\\t/g; s/\n/\\n/g; s/\r/\\r/g; s/\f/\\f/g;
THANKS (I don't think there a fn for this...the closest I saw was quotemeta and I don't think tr/// helps...)

Replies are listed 'Best First'.
Re: Can this be a 1 line or simpler regex?
by Abigail-II (Bishop) on Apr 05, 2003 at 07:30 UTC
    I think the above is fine, but if you insist on less lines:
    %& = ("\t" => '\t', "\n" => '\n', "\r" => '\r', "\f" => '\f'); s/[\t\n\r\f]/$&{$&}/g;

    I wouldn't call it simpler though.

    Abigail

Re: Can this be a 1 line or simpler regex?
by BrowserUk (Patriarch) on Apr 05, 2003 at 08:38 UTC

    Yes, though you might want to lay this out over two lines.

    s{([\t\n\r\f])}{ local $_= '\\' . $1; tr[\t\n\r\f][tnrf]; $_ }eg

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Beautiful. Though, without knowing exactly why, that layout bugs me. I'd rather do
      s{([\t\n\r\f])}{ local $_= $1; tr[\t\n\r\f][tnrf]; "\\$_" }eg;
      Shrug. :)

      Makeshifts last the longest.

        Actually, I think I prefer your layout too--and I can't exactly put my finger on why either:)


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.

        Ew. That that sprinkles eval blocks all over. That's also suboptimal. The original poster had the best version already. I fixed my own code to be nearly similar except that it doesn't benefit from perl's ability to compile a static regex only once. Your code is a static regex but you're imposing eval {} at each match point.

        An entirely different route might be to do some code generation so that the common parts are written only once, eval blocks aren't sprinked all over the place and the world is good again. The only issue is that now its not pretty anymore.

        # Keep $escape somewhere or give it a proper name. my $escape = eval "sub {\n".join("\n",map " \$_[0] =~ s/\\$_/\\\\$_ +/g;", qw/t r n f/)."\n};"; $escape->( $text );
Re: Can this be a 1 line or simpler regex?
by diotalevi (Canon) on Apr 05, 2003 at 07:35 UTC

    (fixed the code)

    I suppose a single regex could be devised for this but it'd be suboptimal compared to what you already have. A short loop would also do this and again, I prefer your original.

    $text =~ s/\\$r/\\\\$r/g for qw/t n r f/;
    for(qw't n r f') { my $r = "\\$_"; $text =~ s/$r/$r/g; }
      How's that supposed to work? As is it's a no-op.

      Makeshifts last the longest.

Re: Can this be a 1 line or simpler regex?
by IndyZ (Friar) on Apr 05, 2003 at 07:28 UTC
    This works:

    s/([\t\n\r\f])/%m=("\t","\\t","\n","\\n","\r","\\r","\f","\\f");$m{$1}/ge;

    but it just feels dirty.

    --
    IndyZ

Re: Can this be a 1 line or simpler regex?
by Aristotle (Chancellor) on Apr 06, 2003 at 00:02 UTC
    $_ = "one\ttwo\nthree four five\r"; s/(\t)|(\n)|(\r)|(\f)/ ($1 && '\t' || '') . ($2 && '\n' || '') . ($3 && '\r' || '') . ($4 && '\f' || '') /ge;
    Update:
    $_ = "one\ttwo\nthree four five\r"; s/(\t)|(\n)|(\r)|(\f)/ $1 ? '\t' : $2 ? '\n' : $3 ? '\r' : $4 ? '\f' : '' /ge;

    Makeshifts last the longest.

Re: Can this be a 1 line or simpler regex?
by Enlil (Parson) on Apr 06, 2003 at 03:13 UTC
    Here is one more way to do it:
    1 while(s!(\n)|(\r)|(\t)|(\f)!('\n','\r','\t','\f')[$#--1]!e);

    -enlil

Re: Can this be a 1 line or simpler regex?
by I0 (Priest) on Apr 06, 2003 at 05:54 UTC
    s/([\t\n\r\f])/${{"\t"=>"\\t","\n"=>"\\n","\r"=>"\\r","\f"=>"\\f"}}{$1}/g;
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found