Regular expression pattern
Regular expressions are one of the thing开发者_运维问答s that still escape me. What I want is simple enough, but I have yet to be able to consistently match. The text I want to match is /ssl/checkoutstep1.aspx
regardless of case.
Instead of the default delimiter /
, it's easier if you use a non-slash like pipe: |
if ($string =~ m|/ssl/checkoutstep1\.aspx|i) {
print 'match';
} else {
print 'no match';
}
I'm assuming you actually need Regex (because you want to learn it, or you are doing a path rewrite, or something). Your example could easilly be solved with simple case-insensitive indexof or contains.
Since it doesn't look like you really need a regular expression, you should consider eq or index.
if ( lc( $string ) eq '/ssl/checkoutstep1.aspx' ) { ... } ## for exact matches
or
if ( index( lc( $string ), '/ssl/checkoutstep1.aspx' ) != -1 ) { ... } ## for partial matches
This is faster and avoids the confusion of regular expressions. If you insist on regular expressions, agent-j's response is what you want, although I prefer {}.
if ( $string =~ m{\Q/ssl/checkoutstep1.aspx\E}i ) { ... } ## the \Q and \E escape the special chars between them
精彩评论