http://www.perlmonks.org?node_id=1027913


in reply to Re: Regex Modification
in thread Regex Modification

This regex results in consecutive number not being matched like:

Not Matched : 1. 123456789 2.123456789147

Pls tell?

Replies are listed 'Best First'.
Re^3: Regex Modification
by AnomalousMonk (Archbishop) on Apr 10, 2013 at 08:05 UTC
    This regex results in consecutive number not being matched like:
    Not Matched : 1. 123456789 2.123456789147
    Pls tell?

    That's because with this regex, a match requires separator characters from the set  [- .] to be present between each three-digit group; this requirement is implied by the specification-by-example in the OP. If you want something like '123456789' to match, try:

    >perl -wMstrict -le "my @numbers = ( '123-456-789', '123.456.789', '123 456 789', '123456789', '999-999.999', '999 999-999', '999.999 999', '9999-999-9999', '99-999-99', '0123456789', '999999-999', '999.999999', ); ;; my $diff = qr{ [-. ] }xms; for my $n (@numbers) { my $match = $n =~ m{ (?<! \d) \d{3} ($diff?) \d{3} \1 \d{3} (?! \d) }xms; print $match ? ' ' : 'NO', qq{ match: '$n'}; } " match: '123-456-789' match: '123.456.789' match: '123 456 789' match: '123456789' NO match: '999-999.999' NO match: '999 999-999' NO match: '999.999 999' NO match: '9999-999-9999' NO match: '99-999-99' NO match: '0123456789' NO match: '999999-999' NO match: '999.999999'

    I'm puzzled by the presence of '123456789147', which you seem to want to match. This is not in accord with the negative look-around assertions in your OP. Can you please clarify: do you want any sequence of nine or more digits to match? If so, where should the separator characters '- .' fall in relation to the digits?

      All rite I am telling the specification for a match:

      1. Number seq. with min 9 and max 15 digits. Like 145-1256-2365-789 2. First 3 and Last 3 should be numbers not diffrentiators. Like 123-1 +545645-123 3. Match can also be without a diffrentiator like 123456789147 4. The diffrentiator(same throughout sequence) can come any number of +times between the first and last 3 digits until the min and max lengt +h criteria is satisfied. Like 1213-456-789-1265

      Pls Help...

        All rite I am telling the specification for a match:

        Full knowledge of a problem if often the first step toward a solution!

        Here's an incomplete solution: incomplete because I feel I should be able to match with strings like
            qw(x123-456789123-456x  x123456-789123456x  x123456789123456x)
        and I can't. In addition, the regex I came up with is quite complicated, probably excessively so.

        Be that as it may, everything else seems to work as intended. The critical portions are the $diff, $d_min and $ndn regexes in the  m1() function. I haven't had time to work on this as I would like, but may do so shortly; it's an interesting problem. Sorry for the delay in getting back to you on this. HTH.

        Output:

Re^3: Regex Modification
by si_lence (Deacon) on Apr 10, 2013 at 07:54 UTC
    How about
    use strict; use warnings; my @nums =( '123-456-789', '123.789.456', '963 456 741', '123-456.789', '123 789-456', '963.456 741', '123456789', '123-456' ); for my $num (@nums) { print "$num "; $num =~ s/[0-9]//g; if ($num =~ m/^([-. ])\1*$|^$/) { print "matches \n"; } else { print "does not match \n"; } }
    If the last number should not match, just delete the * in the regex