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

hi,

at first i wanted this field to only contain alphabetical characters.
but i realized that someone may have a middle name and use it in intials for example first name : bob last name : d. jones

i have this regex for the first and last name fields:
if ($chart_name =~ /^([-\@\w.]+)$/) { #good data } else { die; }


now when I try to add a "." it does not work and dies]

how can i edit this regex so it allows "." and perhaps "-" dashes?

Thanks monks!

Replies are listed 'Best First'.
Re: simple regex for names or last names
by Zaxo (Archbishop) on May 03, 2005 at 04:29 UTC
Re: simple regex for names or last names
by davidrw (Prior) on May 03, 2005 at 03:47 UTC
    "d. jones" won't match because of the space, which isn't allowed by your regex--you are properly allowing periods and dashes.

    As for accepting a "middle. last" value in the last name field, it would probably be better to just have an optional middle initial field.

    Also, keep in mind other characters for last names, such as "O'Hare" and "la Fontaine". (why is '@' an allowed character? Also note you're allowing '_' by using \w)
    Update: As noted by tlm, \w is allowing 0-9 as well (i originally misread OP's "alphabetical" as "alphanumerical" so only mentioned the '_')

      Also note you're allowing '_' by using \w

      ...and decimal digits too.

      the lowliest monk

Re: simple regex for names or last names
by Grygonos (Chaplain) on May 03, 2005 at 12:58 UTC

    I just recently spent a lot of time doing this without the lingua module. I can tell the most important thing you can do is to know your data format. If you post your data format I can help you come up with a few things probably.

      Hello,

      I'm not sure, but is this question just asking to allow a field with aphabetical characters along with a - and . ?

      Of course like davidrw, a name can also contain a '.

      unless ( param('Company Name') =~ /^[\w.\'-]+$/) { print "Invalid characters in name. Only alphanumeric characters and + the following symbols . ' - _ are allowed."; }

      Thanks,
      perleager