in reply to How to count the number of chars in a string

You are not counting characters* but digits \d which might be followed by 0 or more non-numbers \D* .

Not sure why?

I'd suggest better code if I new what your goal is.

Edit
The following example shows how to match (and consequently count) digits or any other character class.

No need for weird negated character classes.

DB<6> x $_='12a3b';/\d/g 0 1 1 2 2 3 DB<7> x $_='12a3b';/[ab]/g 0 'a' 1 'b'

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

*) Character_(computing) include letters, numerical digits, common punctuation marks (such as "." or "-"), and whitespace.

Update

In case you are only counting single characters and not combinations consider using tr instead, like described in Re: Some odd ambiguity in this regex