OnionKnight has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for some way to turn the case-insensitivty on or off inside a regexp. I thought doing something like \i in the pattern would toggle that but it didn't work. Say I have a pattern like: /<a href="http:\/\/somelink.com"/ Then I would like the address to be matched insensitive and the "a href" to sensitive.
  • Comment on Can you have partial case-insensitivty in a regexp?

Replies are listed 'Best First'.
Re: Can you have partial case-insensitivty in a regexp?
by Celada (Monk) on Dec 17, 2005 at 22:49 UTC

    Sure, if you check the perlre manpage you will see that the (?imsx-imsx:pattern) construct will do it:

    /<a href="http:\/\/(?i:somelink.com)"/

    I suggest you use a delimiter other than / because of the leaning toothpick symdrome it creates in this instance. Also, you should keep in mind that HTML tag names and paramater names are not case sensitive, and neither is the protocol name, so you might just want to make the whole pattern case insensitive in this case. It is only the path of the URL after the hostname that is case sensitive.

      In XHTML all the tags and parameter names are case sensitive -> they're all lowercase. I don't know about the protocol name, but sure there's some URI reference where it says it's lowercase. I agree with the leaning toothpick syndrome, you should change your delimiters.
        Right. The URI references isn't a paremeter name though, it's a value, so XHTML says it can have any case. And DNS says that hostnames are case insensitive (AFAIK), so perlmonks.org, PerlMonks.org or pErLmOnKs.oRg are all the same.

        -- zigdon

Re: Can you have partial case-insensitivty in a regexp?
by GrandFather (Saint) on Dec 17, 2005 at 22:45 UTC

    Use (?i):

    use strict; use warnings; my @strs = ( '<a href="http://somelink.com"', '<a href="http://SOMELINK.com"', '<a HREF="http://somelink.com"', ); for my $str (@strs) { print "$str matches\n" if $str =~ m|<a href=(?:(?i)"http://somelink. +com")|; }

    Prints:

    <a href="http://somelink.com" matches <a href="http://SOMELINK.com" matches

    DWIM is Perl's answer to Gödel
A reply falls below the community's threshold of quality. You may see it by logging in.