how to represent it via REL

Discussion in 'Perl Misc' started by Huajian Luo, Nov 9, 2005.

  1. Huajian Luo

    Huajian Luo Guest

    Hi there,

    I need to state the following name convention by perl regular
    expression.

    "valid names are comprised of only alphanumeric characters
    plus the characters '-', '_', and '.'. Names must begin
    with a letter.

    I've tried the following function
    sub valid_name{
    my $name = $_

    if ($name !~ /*[!a-zA-Z0-9_.-]* | [!a-zA-Z]*/) {
    print "Name $name is not valid";
    } else {
    print "MMMMMMMMMMMMMMMMMMMMM";
    }
    but it doesn't work.

    Any hints on t his?
    --
    Thanks,

    Huajian Luo.
     
    Huajian Luo, Nov 9, 2005
    #1
    1. Advertising

  2. Huajian Luo wrote:
    >
    > I need to state the following name convention by perl regular
    > expression.
    >
    > "valid names are comprised of only alphanumeric characters
    > plus the characters '-', '_', and '.'. Names must begin
    > with a letter.


    $name =~ /\A[a-zA-Z][\w.-]*\z/;


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 9, 2005
    #2
    1. Advertising

  3. Huajian Luo

    Huajian Luo Guest

    "John W. Krahn" <> writes:
    > Huajian Luo wrote:
    > >
    > > "valid names are comprised of only alphanumeric characters
    > > plus the characters '-', '_', and '.'. Names must begin
    > > with a letter.

    >
    > $name =~ /\A[a-zA-Z][\w.-]*\z/;


    It works, Thanks for the info.
    --
    Thanks,

    Huajian Luo.
     
    Huajian Luo, Nov 9, 2005
    #3
  4. Huajian Luo

    Huajian Luo Guest

    "John W. Krahn" <> writes:
    > Huajian Luo wrote:
    > >
    > > I need to state the following name convention by perl regular
    > > expression.
    > >
    > > "valid names are comprised of only alphanumeric characters
    > > plus the characters '-', '_', and '.'. Names must begin
    > > with a letter.

    >
    > $name =~ /\A[a-zA-Z][\w.-]*\z/;
    >

    But I have a problem when $name is "s_@_______", I tried qw()
    but it still think it's valid.
    --
    Thanks,

    Huajian Luo.
     
    Huajian Luo, Nov 9, 2005
    #4
  5. Huajian Luo

    Samwyse Guest

    Huajian Luo wrote:
    > "John W. Krahn" <> writes:
    >
    >>$name =~ /\A[a-zA-Z][\w.-]*\z/;
    >>

    >
    > But I have a problem when $name is "s_@_______", I tried qw()
    > but it still think it's valid.


    1) How are you using qw()? It is used for array contants.

    2) "s_@_______" shouldn't match the reg-ex that John gave you. Are you
    sure you copied it correctly?

    3) Show us the code that fails.
     
    Samwyse, Nov 9, 2005
    #5
  6. Huajian Luo

    Scott Bryce Guest

    Huajian Luo wrote:

    > "John W. Krahn" <> writes:
    >
    >>Huajian Luo wrote:
    >>
    >>>"valid names are comprised of only alphanumeric characters
    >>>plus the characters '-', '_', and '.'. Names must begin
    >>>with a letter.

    >>
    >>$name =~ /\A[a-zA-Z][\w.-]*\z/;
    >>

    >
    > But I have a problem when $name is "s_@_______", I tried qw()
    > but it still think it's valid.


    Add these two lines at the beginning of your script:

    use strict;
    use warnings;

    That will help you find the problem. I suspect your script won't
    compile, but the errors and warnings will help you see the problem.

    In case you still can't figure it out, this script demonstrates the problem:


    my $name = "s_@_______";

    print somesub($name), "\n";

    $name = 's_@_______';
    print somesub($name), "\n";

    sub somesub
    {
    return shift;
    }
     
    Scott Bryce, Nov 9, 2005
    #6
  7. Huajian Luo

    Huajian Luo Guest

    Scott Bryce <> writes:
    >
    > use strict;
    > use warnings;
    >
    > That will help you find the problem. I suspect your script won't
    > compile, but the errors and warnings will help you see the problem.

    I just write a test prog to test my function, sorry.

    > In case you still can't figure it out, this script demonstrates the problem:
    >
    >
    > my $name = "s_@_______";
    >
    > print somesub($name), "\n";
    >
    > $name = 's_@_______';
    > print somesub($name), "\n";
    >
    > sub somesub
    > {
    > return shift;
    > }

    Yes, you and John's REL are right the reason is that the string I issued,
    I should Use single quote instead of double quote, to avoid interpolation.

    Thanks for your help.

    --
    Thanks,

    Huajian Luo.
     
    Huajian Luo, Nov 9, 2005
    #7
  8. Huajian Luo

    Bart Lateur Guest

    Huajian Luo wrote:

    >"valid names are comprised of only alphanumeric characters
    >plus the characters '-', '_', and '.'. Names must begin
    >with a letter.
    >
    >I've tried the following function
    >sub valid_name{
    > my $name = $_
    >
    > if ($name !~ /*[!a-zA-Z0-9_.-]* | [!a-zA-Z]*/) {
    > print "Name $name is not valid";
    > } else {
    > print "MMMMMMMMMMMMMMMMMMMMM";
    >}


    Let's work with what you've got... First of all, negating a character
    class happens with "^", not "!". It looks to me like you want to test if
    some characters don't fit the description, then you want it to fail. But
    it appears to me you have too many negations. Reduced:

    $name =~ /[^a-zA-Z0-9_.\-]|^[^a-zA-Z]/

    This will succeed if there's an unacceptable character anywhere, or a
    non-letter at the front. And then you have an unacceptable name.

    --
    Bart.
     
    Bart Lateur, Nov 9, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Islam Elkhayat

    Insert <link rel="stylesheet"....>

    Islam Elkhayat, Nov 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,164
    =?Utf-8?B?V2ViTWF0cml4?=
    Nov 29, 2004
  2. Emil Christopher Melar

    App_Theme and <link rel="stylesheet" .. /> output problems.

    Emil Christopher Melar, Mar 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,688
    Emil Christopher Melar
    Mar 3, 2006
  3. Leeza

    CSS Navigation Rel Link

    Leeza, Jul 28, 2003, in forum: HTML
    Replies:
    5
    Views:
    535
    E-Guy
    Aug 14, 2003
  4. Toby Inkster

    rel="next"

    Toby Inkster, Aug 21, 2004, in forum: HTML
    Replies:
    7
    Views:
    591
    Toby Inkster
    Aug 29, 2004
  5. Mario Krsnic
    Replies:
    0
    Views:
    382
    Mario Krsnic
    Jun 23, 2006
Loading...

Share This Page