smile4me has asked for the wisdom of the Perl Monks concerning the following question:
We all know that "In list context, a regex match returns a list of captured substrings." And, we also know "Numeric quantifiers express the number of times an atom may match. {n} means that a match must occur exactly n times." So can the numeric quantifier work with the captured substrings?
perl -E '$s = q[AAD34017837201D98AAED18778DEF993]; say length($s), " ", $s; @m = $s =~ /(....)(....)(....)(....)(.+)/; say "", join("-",@m);' # 32 AAD34017837201D98AAED18778DEF993 # AAD3-4017-8372-01D9-8AAED18778DEF993
In contrast, the following regex uses a numeric quantifier but does not work as above:
perl -E '$s = q[AAD34017837201D98AAED18778DEF993]; say length($s), " ", $s; @m = $s =~ /(....){4}(.+)/; say "", join("-",@m);' # 32 AAD34017837201D98AAED18778DEF993 # 01D9-8AAED18778DEF993
So, is there a way to use capture groups to match multiple times like separate groups does in the first example?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: mysteries of regex substring matching
by haukex (Bishop) on Jan 15, 2021 at 22:50 UTC | |
Re: mysteries of regex substring matching (updated)
by AnomalousMonk (Bishop) on Jan 15, 2021 at 23:14 UTC | |
by LanX (Cardinal) on Jan 16, 2021 at 11:43 UTC | |
Re: mysteries of regex substring matching
by LanX (Cardinal) on Jan 15, 2021 at 22:34 UTC | |
by LanX (Cardinal) on Jan 15, 2021 at 22:45 UTC | |
by LanX (Cardinal) on Jan 15, 2021 at 22:53 UTC | |
Re: mysteries of regex substring matching
by LanX (Cardinal) on Jan 15, 2021 at 23:15 UTC | |
by LanX (Cardinal) on Jan 16, 2021 at 00:49 UTC |