Discuss Regular expression in perl in the Perl Programming forum on Dev Shed. Regular expression in perl Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
Posts: 3
Time spent in forums: 1 h 4 m 29 sec
Reputation 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>"
Posts: 6
Time spent in forums: 1 h 8 m 58 sec
Reputation 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.
Posts: 6,894
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 49 m 47 sec
Reputation Power: 3885
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";
}