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

SOLUTION: Amazing Regexp Grinder (ARG)

by Dog and Pony (Priest)
on Mar 01, 2002 at 01:55 UTC ( #148450=note: print w/ replies, xml ) Need Help??


in reply to Amazing Regexp Grinder (ARG)

It is no fun to write something you think is clever, unless you can show off with all the nitty-gritty. I was really hoping that someone bored enough would attempt to decode this one, but I decided to post a spoiler myself instead. Hopefully, some people will enjoy the "tricks" I tried to employ - almost all of them are misdirections, trying to make you beleive something else is happening. Did it work? Inquiring minds want to know... anyways, thanks for letting me waste some of your time. Keep on making unmaintainable code out there. :)

#!/usr/bin/perl -wl0040
These switches sets some special variables for me, which I will be using later. It sets $/ to a newline character, and $\ to a space character. See perlrun for details.
use vars qw( $ARG );
Minor thingy that will not fool many, but I don't like being sloppy, can I avoid it; this has no actual effect, but tries to hide the fact that use English; aliases $_ to $ARG.
$ARG = qr(

  /^C^C^C^P\\\[^D"\[^P^C""0"\0\/""\0""!!0""0\0""0\0""0!!\/""
  "0"\[^P^C0\[!^C^P"\[^C0^P\/\\\0"!"0"!"0\["\\"0""0\/^P^C0"
  ""0"\[\[\\\\0"\0/
);
Here is a fun part. This looks very much like a hairy regular expression, yes? Well, I am not gonna use it as a such. Actually, I am using qr instead of q - I am gonna use $ARG as a plain string. I also use the slashes at the start and end (marked with blue) to make it look even more like a regular expression - in reality, they would probably break your otherwise valid regexp if you add them. I will get back to this later, though.

So, what about this match then?

($_=~s/$ARG/JAPH/ ||
  s/(\W)/\$$1/g && s/\$(\s)\$//g && s'([^/]+)'#$1\')
    or warn "No match!";	
When you have figured out that $_ and $ARG are the same, you will expect this to match, and the rest of the line to not execute. Also, if you are a bit gullible, you might suspect that the JAPH part has something to do with the final output. But if you use this "regexp" as a string, it will look like this:
 (?-xism:

  /^C^C^C^P\\\[^D"\[^P^C""0"\0/""\0""!!0""0\0""0\0""0!!/"""
  0"\[^P^C0\[!^C^P"\[^C0^P/\\\0"!"0"!"0\["\\"0""0/^P^C0"""0
  "\[\[\\\\0"\0/
Which will not match the regexp that it forms, so in reality it could just as well say:
(0 || s/(\W)/\$$1/g && s/\$(\s)\$//g
  && s'([^/]+)'#$1\') or warn "No match!";	
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g &&
  s/\$(\s)\$//g && s'([^/]+)'#$1\') or warn "No match!";	
This part is straightforward: it simply adds a dollarsign ($) in front of every non-word character, producing:
 $($?$-xism$:$
$       $
$ $ $/$^C$^C$^C$^P$\$\$\$[$^D$"$\$[$^P$^C$"$"0$"$\0$/$"$"$\
0$"$"$!$!0$"$"0$\0$"$"0$\0$"$"0$!$!$/$"$"$"0$"$\$[$^P$^C0$\
$[$!$^C$^P$"$\$[$^C0$^P$/$\$\$\0$"$!$"0$"$!$"0$\$[$"$\$\$"0
$"$"0$/$^P$^C0$"$"$"0$"$\$[$\$[$\$\$\$\0$"$\0$/$
$)
Notice the highlighted part, because next regexp is just there to take that part out, with the newline. No magic. :)
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g && s/\$(\s)\$//g &&
  s'([^/]+)'#$1\') or warn "No match!";	
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g && print
  && s/\$(\s)\$//g && s'([^/]+)'#$1\') or warn "No match!";	

print qq($') && eval;
The highlighting should explain it - most would expect that I am matching something (actually, everything up till the first forward slash), and then adding a '#' in front of it. And since everything (mostly) seems to close alright, and we even have a warning in case something goes wrong, this should be it. But in reality, I am substituting everything up till said slash with the beginning of the next line too. See the green highlights to see which outer parenthesis actually match too. This makes our $_ look like this at the moment:
 #$1') or warn "No match!";

print qq($/$^C$^C$^C$^P$\$\$\$[$^D$"$\$[$^P$^C$"$"0$"$\0$/$
"$"$\0$"$"$!$!0$"$"0$\0$"$"0$\0$"$"0$!$!$/$"$"$"0$"$\$[$^P$
^C0$\$[$!$^C$^P$"$\$[$^C0$^P$/$\$\$\0$"$!$"0$"$!$"0$\$[$"$\
$\$"0$"$"0$/$^P$^C0$"$"$"0$"$\$[$\$[$\$\$\$\0$"$\0$/)
Which, when evaled, of course interpolates all these variables. Depending on which variables, they have different defaults (they are perl special vars), most of them zero, some are empty (just for show) and like I mentioned in the beginning, I also set $/ to a newline character, and $\ to space. This also made it possible to make \/ look like I was escaping slashes in the "regexp". So in reality, this code looks like this:
print "\n0000   00  000  0  0 \n   0  0  0 0  0 0  0 \n   
0  0000 000  0000 \n   0  0  0 0    0  0 \n000   0  0 0   
 0  0\n";
Which, of course, will print:
0000   00  000  0  0
   0  0  0 0  0 0  0
   0  0000 000  0000
   0  0  0 0    0  0
000   0  0 0    0  0
Please point out if something didn't make sense, was unclear (or somehow wrong), and I'll of course fix it. Any comments also very much appreciated (thanks grinder!).
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Edit: chipmunk 2002-02-28


Comment on SOLUTION: Amazing Regexp Grinder (ARG)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-11-27 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (190 votes), past polls