Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Breaking symmetry with a regex

by BillKSmith (Monsignor)
on Apr 14, 2021 at 14:52 UTC ( #11131278=note: print w/replies, xml ) Need Help??


in reply to Breaking symmetry with a regex

Here is a regex solution. I have provided several test cases which are intended to help you specify your real requirements. (The 'description' field tells what is special about each string.) I have assumed that by 'number', you mean 'unsigned integer'.
use strict; use warnings; use Test::Simple tests=>7; my $NUMBER = qr/[1-9]\d*/; # Unsigned integer sub solution { return $_[0] =~ s! ($NUMBER:$NUMBER): !$1//!xr; } my @test_cases = ( #given expected description ['3:4:', '3:4//', 'minimal'], ['3:4:5:', '3:4//5:', 'begining of string'], ['foo:fum3:4:', 'foo:fum3:4//', 'end of string'], ['3456:7890:5:', '3456:7890//5:', 'big integers'], ['a:b:', 'a:b:', 'no number'], ['3:4foo:', '3:4foo:', 'misplaced colon'], ['3456:0890:a:', '3456:0890:a:', 'invalid integer'], ); foreach my $case (@test_cases) { my ($given, $expected, $description) = @$case; my $computed = solution($given); ok( $computed eq $expected, $description); }

OUTPUT,

1..7 ok 1 - minimal ok 2 - begining of string ok 3 - end of string ok 4 - big integers ok 5 - no number ok 6 - misplaced colon ok 7 - invalid integer

I encourage you to modify this solution and/or list of test cases as your requirements evolve.

Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-05-23 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?