Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: (GOLF) - multiple digit finder regex - 17 chars

by particle (Vicar)
on Feb 02, 2002 at 21:38 UTC ( [id://142965]=note: print w/replies, xml ) Need Help??


in reply to regexp golf - homework

it's not as difficult as you may think. the regex looks like this: /.*?([0-9]).*?\1/. including the brackets, it's 17 characters long! here's the full program:

#!/usr/local/bin/perl -w use strict; $|++; my @numlist; push @numlist, int(rand 100000) for 1..10; for(@numlist) { next if /^$/; /.*?([0-9]).*?\1/ ? print "$_\tmultiple $1\n" : print "$_\tsingle\ +n"; }
i generated a list of random five digit numbers to feed the regex. here's a breakdown of the regex:
.*? i match zero or more characters, non-greedily
([0..9]) match numbers 0..9, save in \1
.*? again match zero or more characters, non-greedily
\1 find the next instance of the character in \1 *

*what's most interesting about this, is that $1 does not work in place of \1. instead, on my perl 5.6.1 install, it warns with:

Use of uninitialized value in concatenation (.) or string at test_matc +h1.pl line 10. Nested quantifiers before HERE mark in regex m/.*?([0-9]).*?+ << HERE +/ at test_match1.pl line 10.
by the way, there's nothing to stop this from working with letters, as well. just change 0..9 and you're on your set.

enjoy!

Update: i see the error of my ways. tilly's right (below). i solved the exact opposite of the problem, which was pretty easy! whoops!

~Particle

Replies are listed 'Best First'.
Re: Re: (GOLF) - multiple digit finder regex - 17 chars
by trs80 (Priest) on Feb 02, 2002 at 22:41 UTC
    It is nice, but it broke the rules :^( The post says no characters other then (),?,|,* this rules out the character class [] I believe. Some body please correct me if I am wrong. My own version that breaks the rules by using \d and not using the regex itself for success, but still a fun brain exercise is this:
    S:while(<DATA>){%d=();while(/(\d)/g){if($d{$1}) {print "DUP: $1 in $_";next S}$d{$1}=1}} __DATA__ 12345 012345 894376 3885437 98374 30924 03284098325 094385098432 74362 876543 4456789
Re: Re: (GOLF) - multiple digit finder regex - 17 chars
by chipmunk (Parson) on Feb 03, 2002 at 04:21 UTC
    /.*?([0-9]).*?\1/ *what's most interesting about this, is that $1 does not work in place of \1.
    Well, of course! $1 and \1 mean different things. When $1 is used in a regex, its value is interpolated as the regex is compiled, just like any other variable. For example:
    'b' =~ /(.)/; print "Yup!\n" if 'ab' =~ /(.)$1/;
    prints Yup! because $1 holds 'b' from the first match and the second regex is compiled as /(.).*c/.

    \1, on the other hand, is special regex syntax, and matches the same thing that was matched by the first capturing group in the current regex.

    'b' =~ /(.)/; print "Yup!\n" if 'ab' =~ /(.)\1/;
    does not print Yup!, because \1 wants to match an 'a'.

    (\1 on the right hand side of a substitution, with the same meaning as $1, has been deprecated for quite some time.)

    Update: Fixed the explanation; an earlier revision of the example used 'c' instead of 'b'.

      (\1 on the right hand side of a substitution, with the same meaning as $1, has been deprecated for quite some time.)

      yeah. that's how i was used to seeing \1. from people learning perl who are used to vi, ex, etc. on the right hand side, it's a no-no.

      although, there's a problem with the explanation of your first example. $1 can't hold 'c', it's nowhere to be found. but i understand the difference now. docs are good.

      ~Particle

Re^2: (GOLF) - multiple digit finder regex - 17 chars
by Anonymous Monk on Dec 11, 2012 at 04:09 UTC
    I think that solving the opposite porblem means solving the original problem. Just filter the input using /\d+/ to insure that they are composed by only digits. Then, solve the problem in the opposite way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2024-03-28 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found