First, there's no need for a triple negative. That's like saying:

my $$$a = \\$b;

just because you can.

And there's no need to put \D in a character class. It is, itself, a character class. Only put it in a character class if you intend to also add other things to it.

And though /g is useful in many situations (even in Scalar context), it's not helping you here because you're simply looking for a nondigit somewhere in the string. Regular expressions look through the whole string automatically, for one instance of a match, and return true as soon as they find that instance. If you had a situation where you cared about multiple matches, you would use /g, but you only want to reject the string if it contains one or more. You don't really care about the "or more" part. One is enough to be a problem, so just get rid of the /g; it wasn't working for you in that example anyway.

So after all's said and done, your code should look like this:

my $match = '\D'; print "non digits found\n" if $str =~ $match;

Hope this helps!

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: matching digits and characters by davido
in thread matching digits and characters by Anonymous Monk

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.