0

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.

5
  • Your effort so far is appreciated... Commented Jul 19, 2011 at 16:26
  • 6
    This case seems too simple to use a regex. Just lowercase the string and check for equality. Commented 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. Commented 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. Commented Jul 19, 2011 at 16:29
  • jjnguy depending on the language, might have a case insensitive compare. Commented Jul 19, 2011 at 16:29

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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).
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).
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.
oh, anchor the regex match! m{^\Q/fixed/string\E\z}i
eq is about twice as fast as the anchored regex on my machine, according to my benchmark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.