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

Breaking symmetry with a regex

by Anonymous Monk
on Apr 13, 2021 at 22:50 UTC ( [id://11131230]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a string that has the form 'a:b:' where 'a' and 'b' are both numbers. How do I drop the second colon, or convert it to // ?

Replies are listed 'Best First'.
Re: Breaking symmetry with a regex
by hippo (Bishop) on Apr 13, 2021 at 22:57 UTC

    Your string as described ends with a colon. All you need is chop - no need for regex:

    use strict; use warnings; use Test::More tests => 1; my $have = 'a:b:'; my $want = 'a:b'; chop $have; is $have, $want;

    🦛

Re: Breaking symmetry with a regex
by BillKSmith (Monsignor) on Apr 14, 2021 at 14:52 UTC
    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
Re: Breaking symmetry with a regex
by syphilis (Archbishop) on Apr 13, 2021 at 23:21 UTC
    Hi,

    If you really want to use a regex:
    C:\>perl -wle "$x = 'a:b:'; $x =~ s/:$//; print $x;" a:b
    Cheers,
    Rob
Re: Breaking symmetry with a regex
by perlfan (Vicar) on Apr 14, 2021 at 03:24 UTC
    >where 'a' and 'b' are both numbers

    require 5.010; #<- from your previous post use strict; use warnings; # meth 1 of ? my $x = 'a:b'; my @x = split(/:/,$x); my $y = join(q{//}, @x); print qq{$y\n}; # meth 2 of ? my @z = sort{ $a <=> $b} @x; my $a = join(q{//}, @z); print qq{$a\n};
    Out:
    a//b a//b
Re: Breaking symmetry with a regex
by Anonymous Monk on Apr 14, 2021 at 20:55 UTC
    split is also very handy in such cases, where the string contains values that are separated by a particular character or characters.
      > split is also very handy

      And to hammer home this point, many mistake the first parameter of split to be a simple string, when in fact it is a regex! Be sure to read what PATTERN will do for you.

      Incidentally, split can also be used as a way to trim.
Re: Breaking symmetry with a regex
by Anonymous Monk on Apr 14, 2021 at 00:28 UTC

    its at the beginning of a large string that may or may not contain other colons

      So how about an example of what you're really talking about? Is it something like '123:45:6789:32:654:87' or what? (Please don't give a 10,000 character long example! :)


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

        > Please don't
        what have you done? :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11131230]
Approved by marto
Front-paged by mtmcc
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found