Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

stuck on regexp

by jimbobn (Novice)
on Aug 25, 2002 at 10:50 UTC ( [id://192656]=perlquestion: print w/replies, xml ) Need Help??

jimbobn has asked for the wisdom of the Perl Monks concerning the following question:

Hi All

I have a field that needs to be checked for validity.
It can only contain a-z A-Z 0-9 . _ -
But  . _ - characters cannot be at the begining of the field, and the field can not be just 0-9 or contain 0-9 as the first character i.e:

OK:
billy.bob
billy_bob
billy-bob
billybob
BILLYBOB
BillyBob
Billy2Bob
BillyBob2

NOT OK:
______
_billybob
1billybob
1111
billy^bob

Basicaly, i need to check a name is vaild before passing it over to the unix command 'useradd'.

I have had a go at it myself and looked at some of the tutorials, but no suck luck :-( Thanks

Replies are listed 'Best First'.
Re: stuck on regexp
by moxliukas (Curate) on Aug 25, 2002 at 11:02 UTC

    Well, FreeBSD 'adduser' IIRC displays the regexp that the username must conform to, but I am not at my FreeBSD box now so I can't check that, however here is my stab at a regexp you might need:

    /^[A-Za-z][-.A-Za-z0-9_]+$/

    This will force the first character to be a-z or A-Z and any other character a-z, A-Z, 0-9, -, . or _. It will also force the login name to be at least 2 characters (I'm not sure if this is what you want) and reject a username made up entirely of numbers (as the first character must be a-z or A-Z)

    Hope this helps.

      thanks, that sounds good. i'll give it a go
      cheers guys.
      how would i apply the maximum/minimum regexp to this expression:  if ($email !~ /.+\@.+\..+/) { ?

        Use  .{2,10} instead of  .+ (if I did understand your question)

Re: stuck on regexp
by hotshot (Prior) on Aug 25, 2002 at 11:13 UTC
      thanks for that guys.
      hotshot, is your rexexp missing out a _ or does the \w cater for that?

      Just out of interest, is it possible to specify the minimum/maximum lengh of a variable using a regexp?
      i.e, if i want the username to be no longer than 30 characters.

        To make the username no longer than 30 characters use this:

        /^[A-Za-z][-.\w]{1,29}$/

        This will match when the first character is [A-Za-z] and is followed by 1 to 29 characters of form [-.\w] (thus making the total length of username from 2 to 30 characters)

        And yes, \w caters for [a-zA-Z0-9_]

        \w stands for a-z, A-Z, 0-9 and the underscore (_) os it answers your question.
        about your second question, of course you can limit the string's length, for example if a username length should be maximum 30 characters long than this will qualify:
        /^[A-Za-z][-.\w]{29}$/
        the first char is mandetory to be alphabetic and the 29 in braces replaces the '+' sign to note exact number of chars.

        Hotshot
Re: stuck on regexp
by erikharrison (Deacon) on Aug 25, 2002 at 15:42 UTC

    What abut Unicode? Do you allow accented characters and the like? If so, then the standard range character classes are an outright bug (to loosely paraphrase Larry). Use the POSIX character classes for this.

    Here is a regex using look ahead assertions and POSIX character classes which meets all of these requirements

    $username = <>; print "Yay!\n" if $username =~ /(?! #look ahead . . . ^ #and see if at the start of the st +ring \d+ #we have one or more digits $) #until the end of the string. If n +ot . . . ^ #at the start of the string [[:alpha:]] #allow an alphabetic character [_.-0-9[:alpha:]] #followed by an alphanumeric cha +racter, underscore, dot, or dash {0,29} #0 to 29 times $ #to the end of the string /x;

    Note: That if Unicode character are really not allowed (ie you actually DO want ASCII A-Za-z) then replace [:alpha:] in the example appropriately

    Update: Not long after posting this I realized some mild errors if had because of an incomplete reading of jimbobn's original question. $face = red. This has been corrected Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found