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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.