in reply to Re^3: Empty pattern in regex
in thread Empty pattern in regex

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