I changed my mind about sitting this one out because this code from choroba is so fascinating, and worth exploring, as noted below.

#!/usr/bin/env -S perl #use warnings; # suppress uninitialized warnings use strict; use feature qw{ say }; use experimental qw( signatures ); $_ = 'abXXXXacVVVVVad'; #$_ = 'abacad'; my $lim = 7; say "Examining \'$_\' $lim times"; say "/a(.)/"; if (/a(.)/g) { #say "/q(.)/"; # switch to see no-match example #if (/q(.)/g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } for my $try (1 .. $lim) { say "//"; if (//g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } } $_ = '37.5BBBBB98UUUUU4.075QQQQQ42TTTT0.357SSS'; $lim = 5; say "\nExamining \'$_\' $lim times"; say '/[^\d.](\d+(?:\.?\d*)?)/g'; if (/[^\d.](\d+(?:\.?\d*)?)/g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } for my $try (1..$lim) { say "//"; if (//g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } } exit(0); __END__
  O U T P U T
Examining 'abXXXXacVVVVVad' 7 times /a(.)/ $1: b $&: ab // $1: c $&: ac // $1: d $&: ad // No match // $1: b $&: ab // $1: c $&: ac // $1: d $&: ad // No match Examining '37.5BBBBB98UUUUU4.075QQQQQ42TTTT0.357SSS' 5 times /^\d.(\d+(?:\.?\d*)?)/g $1: 98 $&: B98 // $1: 4.075 $&: U4.075 // $1: 42 $&: Q42 // $1: 0.357 $&: T0.357 // No match // $1: 98 $&: B98

S O M E  O B S E R V A T I O N S
The only use case I can infer from these code tweaks is the notion of determining the number of 'things' present in a string, as long as at least one of those 'things' is present, since there must be at least one match before //g can work its magic. I deliberately omitted a leading non-decimal in the second $_ to force the first match deeper in the string, but that's cool because as long as at least one 'thing' sub-expression is present, //g will find the rest (to the right). Also, note how the entire process starts over from the beginning if the number of $trys exceeds the number of 'things' present, as in $try (1..$lim) {. There must be other use cases for //g;. Any thoughts on how to exploit this facility?

Will


In reply to Re^4: Empty pattern in regex by perlboy_emeritus
in thread Empty pattern in regex by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.