Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex Modification

by Athanasius (Archbishop)
on Apr 10, 2013 at 06:57 UTC ( [id://1027906]=note: print w/replies, xml ) Need Help??


in reply to Regex Modification

Use \1 to match the same delimiter:

#! perl my @should_match = ( '123-456-789', '123.789.456', '963 456 741', ); my @should_not_match = ( '123-456.789', '123 789-456', '963.456 741', ); my $re = qr/\d{3}([ .-])\d{3}\1\d{3}/; for (@should_match) { if ($_ =~ $re) { print "ok\n"; } else { print "failed\n"; } } for (@should_not_match) { if ($_ =~ $re) { print "failed\n"; } else { print "ok\n"; } }

Output:

16:53 >perl 603_SoPW.pl ok ok ok ok ok ok 16:55 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Regex Modification
by Anonymous Monk on Apr 10, 2013 at 07:29 UTC

    This regex results in consecutive number not being matched like:

    Not Matched : 1. 123456789 2.123456789147

    Pls tell?

      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...

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-16 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found