Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Can this be a 1 line or simpler regex?

by BrowserUk (Patriarch)
on Apr 05, 2003 at 08:38 UTC ( [id://248272]=note: print w/replies, xml ) Need Help??


in reply to Can this be a 1 line or simpler regex?

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.

Replies are listed 'Best First'.
Re^2: Can this be a 1 line or simpler regex? (slightly better?)
by Aristotle (Chancellor) on Apr 06, 2003 at 02:23 UTC
    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 );
        you're imposing eval {} at each match point
        Sorry, no. You're confusing /e with /ee. There's no evaling with /e.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found