[Purdue-pm] Perl 6 regexes

Mark Senn mark at purdue.edu
Wed Jun 13 07:17:05 PDT 2018

Perl 5 has regular expressions that can be used to match character
strings.  These have been MUCH improved and are known as regexes in Perl 6.

Below is a message I sent to the Perl6 users mailing list about how to
remove leading zeros.  Thought you might be interested in it, especially
how to use name captures in the last example.

To: Norman Gaywood <ngaywood at une.edu.au>
cc: perl6-users <perl6-users at perl.org>
Subject: Re: How do I remove leading zeros?
Date: Wed, 13 Jun 2018 10:02:43 -0400
From: Mark Senn <mark at 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.


More information about the Purdue-pm mailing list