Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Regular Expression: search two times for the same number of any signs

by AnomalousMonk (Archbishop)
on Nov 29, 2016 at 17:18 UTC ( [id://1176838]=note: print w/replies, xml ) Need Help??


in reply to Regular Expression: search two times for the same number of any signs

Here's another single-regex approach, although as others have said, I don't necessarily think such an approach is best. (Requires Perl version 5.10+.)

Code:

use 5.010; # needs (?(?{ code... })true-pattern) and (*F) extensions use warnings; use strict; use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' # safer to use done_testing() ; use Test::NoWarnings; # use Data::Dump qw(dd pp); # BEGIN { use_ok 'SomeModule', qw(some functions); } # test datasets #################################################### use constant TEST_VECTOR_SET_1 => ( "no repeating groups found", [ '', ], [ 'A', ], [ 'AA', ], [ 'AbA', ], [ 'ABC', ], "one group found", [ 'AAA', 'AAA' ], [ 'AbAcA', 'AbAcA' ], [ 'xAbAcA', 'AbAcA' ], [ 'AbAcAx', 'AbAcA' ], [ 'xAbAcAx', 'AbAcA' ], [ 'AbcAdeA', 'AbcAdeA' ], [ 'xAbcAdeA', 'AbcAdeA' ], [ 'AbcAdeAx', 'AbcAdeA' ], [ 'xAbcAdeAx', 'AbcAdeA' ], [ 'BcBdeBAAA', 'AAA' ], "two or more groups found", [ 'AAAAAA', 'AAA', 'AAA', ], [ 'AbAcAAbAcA', 'AbAcA', 'AbAcA', ], [ 'AbAcAAdeAfgA', 'AbAcA', 'AdeAfgA', ], [ 'AbAcABdeBfgBChijCklmC', 'AbAcA', 'BdeBfgB', 'ChijCklmC', ], [ 'AbAcA BdeBfgB ChijCklmC', 'AbAcA', 'BdeBfgB', 'ChijCklmC', ], [ 'AAbAcAABdeBfgBBCChijCklmCC', 'AbAcA', 'BdeBfgB', 'ChijCklmC', ], "some test cases from pm#1176775 post", [ 'x1x2x...x', 'x1x2x', '...', ], # pm#1176786 ); # functions under test ############################################# sub periodic_1 { my ($string, ) = @_; my @found; push @found, $1 while $string =~ m{ ( # capture entire rep group to $1 (.) # capture rep char to $2 ((?: (?: (?! \2) .)* \2)) # capture 1st rep to $3 ((?: (?: (?! \2) .)* \2)) # capture 2nd rep to $4 (?(?{ length($3) != length($4) }) (*F)) # same rep lengths ) }xmsg; return @found; } # testing, testing... ############################################## FUNT: for my $ar_funt ( # function # name comment [ 'periodic_1', ], ) { my ($func_name, $func_note) = @$ar_funt; *periodic = do { no strict 'refs'; *$func_name; }; defined $func_note ? note "\n $func_name() -- $func_note \n\n" : note "\n $func_name() \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, @expected) = @$ar_vector; my $cmnt_str = join ' ', map "'$_'", @expected; is_deeply [ periodic($string) ], [ @expected ], "'$string' -> ($cmnt_str)", ; } # end for VECTOR } # end for FUNT note "\n done testing functions \n\n"; done_testing(); exit;
Output:
c:\@Work\Perl\monks\Anonymous Monk\1176775>perl periodic_reps_1.pl # # periodic_1() # # no repeating groups found ok 1 - '' -> () ok 2 - 'A' -> () ok 3 - 'AA' -> () ok 4 - 'AbA' -> () ok 5 - 'ABC' -> () # one group found ok 6 - 'AAA' -> ('AAA') ok 7 - 'AbAcA' -> ('AbAcA') ok 8 - 'xAbAcA' -> ('AbAcA') ok 9 - 'AbAcAx' -> ('AbAcA') ok 10 - 'xAbAcAx' -> ('AbAcA') ok 11 - 'AbcAdeA' -> ('AbcAdeA') ok 12 - 'xAbcAdeA' -> ('AbcAdeA') ok 13 - 'AbcAdeAx' -> ('AbcAdeA') ok 14 - 'xAbcAdeAx' -> ('AbcAdeA') ok 15 - 'BcBdeBAAA' -> ('AAA') # two or more groups found ok 16 - 'AAAAAA' -> ('AAA' 'AAA') ok 17 - 'AbAcAAbAcA' -> ('AbAcA' 'AbAcA') ok 18 - 'AbAcAAdeAfgA' -> ('AbAcA' 'AdeAfgA') ok 19 - 'AbAcABdeBfgBChijCklmC' -> ('AbAcA' 'BdeBfgB' 'ChijCklmC') ok 20 - 'AbAcA BdeBfgB ChijCklmC' -> ('AbAcA' 'BdeBfgB' 'ChijCklmC') ok 21 - 'AAbAcAABdeBfgBBCChijCklmCC' -> ('AbAcA' 'BdeBfgB' 'ChijCklmC' +) # some test cases from pm#1176775 post ok 22 - 'x1x2x...x' -> ('x1x2x' '...') # # done testing functions # 1..22 ok 23 - no warnings 1..23


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://1176838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found