in reply to regex elegance contest - validate a pw

Your test data haven't considered that \w also matches '_' , so this is an allowed character in your passwords - is that what you want ?

From perlre

In addition, Perl defines the following: \w Match a "word" character (alphanumeric plus "_")

+++++++++++++++++
#!/usr/bin/perl
use warnings;use strict;use brain;

  • Comment on Re: regex elegance contest - validate a pw

Replies are listed 'Best First'.
Re: Re: regex elegance contest - validate a pw
by tsee (Curate) on Feb 12, 2004 at 12:57 UTC

    Well, isn't \w also locale aware?
    Example: (Germany)

    % perl -e 'use locale; $_=q{äöü}; print qq{Locale aware!\n} if /^\w+$/ +;'

    Results in:
    Locale aware!

    Thus, \w is bad when you really mean /[A-Za-z0-9]/. On the other hand, using the latter character class instead of \w is usually more of a problem for most applications. The best example is, I daresay, entering my last name in some form on the web that does this: Müller ;-)

    Steffen

      I think it is also Unicode aware in 5.8+ so that introduces all sorts of variation - if Unicode is supported, \w usually refers to all alphanumberics - in fact you might see \w start to die out and things like unicode properties appear

      $word = m/([\p{Letter}\p{Number}]+)/;

      I think that is correct...

      I know that in Java's regex package, \w == \A-Za-z0-9_\ only

      I got most of this info from Friedl's Mastering Regular Expressions - if you dont own it - buy it and read it from chapters 1-7 - it is truely excellent, a standout book.

      +++++++++++++++++
      #!/usr/bin/perl
      use warnings;use strict;use brain;

        leriksen says:
        ... Friedl's Mastering Regular Expressions - if you dont own it - buy it ...
        Yeah leriksen, perhaps you should take your own advice and buy a copy. That way I could have my copy back! :-)

        Cheers!