Regular expressions are one of the things 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.
-
Your effort so far is appreciated...Daniel Hilgarth– Daniel Hilgarth2011-07-19 16:26:54 +00:00Commented Jul 19, 2011 at 16:26
-
6This case seems too simple to use a regex. Just lowercase the string and check for equality.jjnguy– jjnguy2011-07-19 16:28:24 +00:00Commented Jul 19, 2011 at 16:28
-
The language is standard perl, but the implementation is not for a language but rather an A/B testing interface, so I just need the pattern itself.S16– S162011-07-19 16:28:48 +00:00Commented Jul 19, 2011 at 16:28
-
My effort so far has been fruitless. As for this case being to simple, it's not a matter of finding the right tool for the match; it's a matter of being required to use regex.S16– S162011-07-19 16:29:33 +00:00Commented Jul 19, 2011 at 16:29
-
jjnguy depending on the language, might have a case insensitive compare.Yuriy Faktorovich– Yuriy Faktorovich2011-07-19 16:29:44 +00:00Commented Jul 19, 2011 at 16:29
Add a comment
|
2 Answers
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.
Comments
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
5 Comments
hobbs
I'm not convinced that it's faster, since
lc has to make a copy of a (potentially large) string, and m//i uses a reasonably quick bitmap-based method to do case-insensitive searches (at least when not in Unicode mode).gpojd
I have benchmarked it before, but that was with an older version of perl. I think the larger the string, the slower the regex (the regex shown, not one with anchors).
gpojd
I benchmarked them again and index/eq almost always beat the regex. The only case that it doesn't is when the string is long AND the match is at the very beginning.
hobbs
oh, anchor the regex match!
m{^\Q/fixed/string\E\z}igpojd
eq is about twice as fast as the anchored regex on my machine, according to my benchmark.