develooper Front page | perl.perl6.users | Postings from June 2018

Re: How do I remove leading zeros?

Thread Previous
From:
Mark Senn
Date:
June 13, 2018 15:14
Subject:
Re: How do I remove leading zeros?
Message ID:
29455.1528898563@pier.ecn.purdue.edu
If $x is set to "01.02.03"

(Not using normal message quoting to make message shorter.
[1] ToddAndMargo    [2] Norman Gaywood    [3] Mark Senn)

DOING         $x ~~ s:global/"0"(\d)/ $0 /;                              [1]
SETS $x TO    "1 . 2 . 3"                                                [1]
COMMENT       Spaces on right hand side of s command are significant.    [2]

DOING         $x ~~ s:global/"0"(\d)/$0/;                                [2]
SETS $x to    "1.2.3"                                                    [2]
COMMENT       Zeroes would de deleted from "101.102.103".                [3]

(I suspect the general case is delete leading zeroes or zeroes
immediately following periods.)

DOING         $x ~~ s:g/(^|".")0+/$0/;                                   [3]
              or, using named captures,
              $x ~~ s:g/$<leader>=(^|".")0+/$<leader>/;
SETS $x to    "1.2.3"                                                    [3]
COMMENT       Zeroes would not be deleted from "101.102.103".            [3]

For me, the last solution is the clearest: replace all beginning of
strings or "."  followed by one or more zeroes by the beginning of the
string or ".", whichever was matched.

-mark

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About