Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

japhy has met his match

by japhy (Canon)
on May 25, 2001 at 18:18 UTC ( #83316=perlmeditation: print w/ replies, xml ) Need Help??

I came up with the worst idea this morning, around 8:54 AM EDT.
(from #irc.linux.com::perl) <japhy> wow. <japhy> no, I just gave myself a difficult challenge. <evilgwynie> what is that? <japhy> $regex =~ $regex; <japhy> create a pattern that matches itself, and NOTHING ELSE <evilgwynie> hmm
Ugh. What did I get myself into?! I labored for an hour or so, and I've not been able to get anything concrete.

One large problem is the exponential increase of backslashes:

to match you need backslashes -------- -------- ----------- ^ \^ 1 \^ \\\^ 3 \\\^ \\\\\\\^ 7 \\\\\\\^ \\\\\\\\\\\\\\\^ 15
That pattern is, of course, 2**n - 1. It is super icky.

japhy -- Perl and Regex Hacker

Comment on japhy has met his match
Select or Download Code
Re: japhy has met his match
by Masem (Monsignor) on May 25, 2001 at 19:08 UTC
    As fuel for the fire, here's a regex that does match against itself, and only a small number of other cases besides itself. I know it doesn't satisify the "only itself" bit, but it could help get others thinking in the right direction:

    #Note that even with single quotes, '\\' is #interpolated to '\'; this doesn't break the regex, #as both sides of =~ will see only '\'. $regex = '^[24\\\\\[\]\{\}\^\$]{24}$'; $text = '2' x 24; # will also match, for example.

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      That's the type of approach I took. I try to beef it up, then, by using (?<=...) to require that the text used in the regex so far MUST be there. Oh, and if were to find a regex, it can't use $ -- it must use \z, since $ can match before a newline at the end of a string.

      japhy -- Perl and Regex Hacker
        Replacing $ with \z is trival:
        $regex = '^[z26\\\\\[\]\{\}\^\$]{26}\z';
        I've also worked out what the 'meta match' is for this expression (that is, $re2 below will only match $regex):
        $re2 = '^\^\\[z26\\\\\\\\\\\\\[\\\\\]\\\\\{\\\\\}\\\\\^\\\\\$\\]\\{26\ +\}\\\\z\z';
        So, the idea is your want, in the regex, to have $re2 attempt to match $regex, then have $regex left around that basically is a large character class with a {n} modifier (Thus, if we need to add any new characters like '(', ')', or '?', we work with $regex to add them, modify $re2 above, and we're set).

        However, I can't figure out how to do this part. I'm still thinking about it, but this is a rather interesting problem.


        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
solution to: Re: japhy has met his match
by japhy (Canon) on May 25, 2001 at 19:56 UTC
    Ok, updated AGAIN, thanks to MeowChow pointing out the obvious. ;)
    123456789_123456789_123456789_123456789_123456789_123456 $re = q{^.*(??{$&ne'^.*(??{'.substr($re,7,-4).'})\z'&&'(?!)'})\z};


    japhy -- Perl and Regex Hacker
      Close, but not quite yet:

      First, here's a 5.005 version that's the same thing:

      $regex = '^(.*)(?(?{my$x=substr$1,9,-4;$1ne"^(.*)(?(?{$x}))\\z"}))\z';
      And here's the stinger:
      $test = '^(.*)(?(?{my$japhy;my$x=substr$1,9,-4;$1ne"^(.*)(??{$x}))\\z +"}))\z';
      (Mind you, I'm playing with this on 5.005, and things that I don't expect to match are matching - I wonder if there's a bug in the RE engine at this point. However, I'm pretty sure that adding any text prior to "my$x..." will still match...)

      I think that you need to specify the exact length of the substr that you're looking at, which means to just simply do some character counting.


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Am I missing something?
      print q|^.*(??{ })\z| =~ m|^.*(??{$&ne'^.*(??{'.substr($&,7,36).'})\z'})\z| # outputs 1
      Update to japhy's update: In that case, why not simply:
      $re = q{^.*(??{$&ne$re&&'(?!)'})\z};
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
Re: japhy has met his match
by bikeNomad (Priest) on May 25, 2001 at 20:27 UTC
    I don't know what you mean by "and NOTHING ELSE", but wouldn't this do the trick?
    my $a = 'a'; print $a =~ $a; # prints 1
      No, because the regex 'a' also matches 'this is a line'. The idea was to only match itself, and no other string.

      The 15 year old, freshman programmer,
      Stephen Rawls
Re: japhy has met his match
by ozzy (Sexton) on May 26, 2001 at 04:36 UTC
    You have way to much time on your hands : )

    -= Ozzy =-
Re: japhy has met his match
by Zaxo (Archbishop) on May 26, 2001 at 10:53 UTC
    use Magic; tilt() if ($str =~ m/$Magic::selfish/ && ${str}abc !~ m/$Magic::selfish/); tilt() if ($Magic::selfish =~ m/$str/ && ${Magic::selfish}abc !~ m/$str/);
    Matching is a one-to-many operation. More succintly:
    die "42" if ${Magic::selfish}z !~ m/$Magic::selfish/;
    You're tilting at a windmill.

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-08-23 01:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (169 votes), past polls