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

Re^2: Using regex to match double letters, and only double letters

by AnomalousMonk (Archbishop)
on Apr 18, 2018 at 01:36 UTC ( [id://1213097]=note: print w/replies, xml ) Need Help??


in reply to Re: Using regex to match double letters, and only double letters
in thread Using regex to match double letters, and only double letters

Further to AnonyMonk's Re^2: Using regex to match double letters, and only double letters regex:   Note that the look-ahead is not strictly necessary:
    / (.) \g-1 (?: \g-1+ (*SKIP)(*FAIL))? /x
works | seems to work equally well. Since Perl version 5.10+ regex extensions must be used (for (*SKIP) (*FAIL)) anyway, I've also used \gn relative backreferencing so the regex can be defined in a  qr// more safely; this can be more convenient in an extractive application.

File only_double_letters_2.pl:
use 5.010; # needs: \gn (*SKIP) (*FAIL) use warnings; use strict; use Test::More 'no_plan'; use Test::NoWarnings; # use Data::Dump qw(dd); use constant TEST_VECTOR_SET_1 => ( 'no matches shall be found', [ '' => qw() ], # empty qw() not needed: documentation only [ 'a' => qw() ], [ 'ab' => qw() ], [ 'aba' => qw() ], [ 'abab' => qw() ], [ 'abcba' => qw() ], [ 'aaa' => qw() ], [ 'aaaa' => qw() ], [ 'baaa' => qw() ], [ 'aaab' => qw() ], [ 'baaab' => qw() ], [ 'aaabbb' => qw() ], [ 'world' => qw() ], 'one or more matches shall be found', [ 'aa' => qw(aa) ], [ 'xaa' => qw(aa) ], [ 'aax' => qw(aa) ], [ 'xaax' => qw(aa) ], [ 'aabb' => qw(aa bb) ], [ 'xaabb' => qw(aa bb) ], [ 'aabbx' => qw(aa bb) ], [ 'xaabbx' => qw(aa bb) ], [ 'xaabbx' => qw(aa bb) ], [ 'aaxbb' => qw(aa bb) ], [ 'xaaxbb' => qw(aa bb) ], [ 'aaxbbx' => qw(aa bb) ], [ 'xaaxbbx' => qw(aa bb) ], [ 'aabbcc' => qw(aa bb cc) ], [ 'xaabbcc' => qw(aa bb cc) ], [ 'aabbccx' => qw(aa bb cc) ], [ 'xaabbccx' => qw(aa bb cc) ], [ 'aaxbbcc' => qw(aa bb cc) ], [ 'xaaxbbcc' => qw(aa bb cc) ], [ 'aaxbbccx' => qw(aa bb cc) ], [ 'xaaxbbccx' => qw(aa bb cc) ], [ 'xaaxbbxccx' => qw(aa bb cc) ], [ 'hello' => qw(ll) ], [ 'balloon' => qw(ll oo) ], [ 'subbookkeeper' => qw(bb oo kk ee) ], ); # testing, testing... ############################################## note "\n test basic regexes head-to-head \n\n"; VECTOR: for my $ar_vector (TEST_VECTOR_SET_1) { if (not ref $ar_vector) { # comment string if not vector ref. note $ar_vector; next VECTOR; } my ($string) = @$ar_vector; ok $string =~ /(.)\1((?!\1)|\1*(*SKIP)(*FAIL))/ eq $string =~ / (.) \g-1 (?: \g-1+ (*SKIP)(*FAIL))? /x, qq{'$string'}, ; } # end for VECTOR done_testing(); exit;
Output:
c:\@Work\Perl\monks\Anonymous Monk\1213014>perl only_double_letters_2. +pl # # test basic regexes head-to-head # # no matches shall be found ok 1 - '' ok 2 - 'a' ok 3 - 'ab' ok 4 - 'aba' ok 5 - 'abab' ok 6 - 'abcba' ok 7 - 'aaa' ok 8 - 'aaaa' ok 9 - 'baaa' ok 10 - 'aaab' ok 11 - 'baaab' ok 12 - 'aaabbb' ok 13 - 'world' # one or more matches shall be found ok 14 - 'aa' ok 15 - 'xaa' ok 16 - 'aax' ok 17 - 'xaax' ok 18 - 'aabb' ok 19 - 'xaabb' ok 20 - 'aabbx' ok 21 - 'xaabbx' ok 22 - 'xaabbx' ok 23 - 'aaxbb' ok 24 - 'xaaxbb' ok 25 - 'aaxbbx' ok 26 - 'xaaxbbx' ok 27 - 'aabbcc' ok 28 - 'xaabbcc' ok 29 - 'aabbccx' ok 30 - 'xaabbccx' ok 31 - 'aaxbbcc' ok 32 - 'xaaxbbcc' ok 33 - 'aaxbbccx' ok 34 - 'xaaxbbccx' ok 35 - 'xaaxbbxccx' ok 36 - 'hello' ok 37 - 'balloon' ok 38 - 'subbookkeeper' 1..38 ok 39 - no warnings 1..39


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found