#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Regular expression in perl


    Hi
    I am trying to have some regular expressions execution in perl but its not working as I expect
    For example, in the following snippet

    my $str = '/8B362F655DF011F60A4A579B94434EA8/employee/21';
    if ( $str =~m!/8B362F655DF011F60A4A579B94434EA8/employee!) {
    print "We found Pete or Steve!\n";
    }

    The regular expression "8B362F655DF011F60A4A579B94434EA8/employee" matches even for the value "/8B362F655DF011F60A4A579B94434EA8/employee/21'"

    I wanted a regular expression which does strict matching like for the regular expression "/8B362F655DF011F60A4A579B94434EA8/employee" it should match only "/8B362F655DF011F60A4A579B94434EA8/employee"
    and for the regex ""/8B362F655DF011F60A4A579B94434EA8/employee/([^/]*)" it should only value "/8B362F655DF011F60A4A579B94434EA8/employee/<atleaseOneShoudComeWithoutSlash>"

    What is the regular expressions should I use ?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    The above code is working fine...

    for a strict matching pattern you can match '/' (you did the same thing..!!) character. or if this character comes always on the first position then use '^' (for more strict matching) at the begging.
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    The regular expression matches if its contents can be found anywhere in the string. In the snippet you posted, if you want the string to end after 'employee', you have to say so, using the $ character that matches the end of the string, i.e.:
    Code:
    if ( $str =~m!/8B362F655DF011F60A4A579B94434EA8/employee$!) {
    print "We found Pete or Steve!\n";
    }
    However, you're not actually using a pattern there at all. What you're looking for there is basically string equality so using a regexp at all is overkill. It could be accomplished more easily using:
    Code:
    if ( $str eq '/8B362F655DF011F60A4A579B94434EA8/employee' ) {
    print "We found Pete or Steve!\n";
    }

    Comments on this post

    • Laurent_R agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Thanks for the reply.
    What regular expression should I use to match
    /8B362F655DF011F60A4A579B94434EA8/employee/<HereSomeThingShouldComeIWthoutSlash>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    As Ishnid said, you don't need reguar expression for what you want. Test for equality of the strings with the eq operator.

    Or, if you want something else, please explain further.

IMN logo majestic logo threadwatch logo seochat tools logo