Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Some odd ambiguity in this regex

by johngg (Canon)
on Jun 05, 2020 at 17:09 UTC ( #11117732=note: print w/replies, xml ) Need Help??


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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117732]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2023-02-06 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (34 votes). Check out past polls.

    Notices?