http://qs1969.pair.com?node_id=11117732


in reply to Some odd ambiguity in this regex

I'm trying to count digits in a scalar

Perhaps you could use tr///, see Quote Like Operators.

johngg@shiraz:~/perl/Monks$ perl -E '$_ = q{1223w3433.45+34}; say tr{0 +-9}{};' 12

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Some odd ambiguity in this regex
by AnomalousMonk (Archbishop) on Jun 05, 2020 at 19:50 UTC
    ... I'm trying to count digits in a scalar

    That's what the OP says, but the  /\d\D*/g regex suggests misterperl is trying to count occurrences of some kind of digit-group pattern. If that's the case, I can, offhand, think of some  s/// approaches (in the vein of a  tr/// operation) that would do the trick as well as the  m// approach that misterperl seems to favor:

    c:\@Work\Perl\monks>perl -wMstrict -le "$_ = '+++1223w3433.45+34***'; ;; my $ndg; ;; $ndg =()= /\d\D*/g; print qq{A: $ndg digit grps.; m// change unpossible '$_'}; ;; $ndg = do { (my $r = $_) =~ s/\d\D*//g }; print qq{B: $ndg digit grps.; s/// string unchanged '$_'}; ;; $ndg = s/(\d\D*)/$1/g; print qq{C: $ndg digit grps.; s/// string unchanged '$_'}; ;; $ndg = s/\d\D*//g; print qq{D: $ndg digit grps.; s/// string CHANGED '$_'}; " A: 12 digit grps.; m// change unpossible '+++1223w3433.45+34***' B: 12 digit grps.; s/// string unchanged '+++1223w3433.45+34***' C: 12 digit grps.; s/// string unchanged '+++1223w3433.45+34***' D: 12 digit grps.; s/// string CHANGED '+++'
    My own preference would be for the  m// approach.


    Give a man a fish:  <%-{-{-{-<