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

There are other nodes out there on this and the last one I read had pretty much the same thing as me but for some reason it doens't work.
$name =~ s/\<name\>//i;
The < and > are intentional, but the /i doesn't match <NAME> or <NaMe>, all it does is match <name> and nothing else. How can I fix this?

Replies are listed 'Best First'.
Re: case insensitive matching
by Caron (Friar) on Feb 14, 2004 at 20:34 UTC

    Your regex does what you want. Perhaps you forgot to apply it to several occurrences of <name>, using the /g modifier.

    #!/usr/bin/perl -w use strict; my $name = "There is a <NaMe> in this string"; $name =~ s/\<name\>/(*)/i; print "$name\n"; $name = "There is a <name> and another <NaMe> in this string"; $name =~ s/\<name\>/(*)/i; print "$name\n"; $name = "There is a <name> and another <NaMe> in this string"; $name =~ s/\<name\>/(*)/gi; print "$name\n"; __END__ __OUTPUT__ There is a (*) in this string There is a (*) and another <NaMe> in this string There is a (*) and another (*) in this string

    (I added a (*) to your regex to show where the replacement was taking place.)

    Ther first time, your regex worked fine, because <NaMe> was the first and only occurrence. The second time it failed because there were two occurrences. The third time, with the /g modifier, it matched both names.

Re: case insensitive matching
by tachyon (Chancellor) on Feb 14, 2004 at 20:37 UTC

    Actually it does work. Perhaps this is not your exact code, perhaps you just think it does not work. Perhaps you have <Name   > ie spaces after the token? If this is HTML or even if not s/<\s*name\s*>//ig may be better. Note you don't have a /g so will only do the first instance of <name> in a given string so perhaps that is your issue?

    my @ary = qw[<name> <Name> <NaMe> ]; for (@ary) { print; s/<name>/japh!/i; print " $_\n"; } __DATA__ <name> japh! <Name> japh! <NaMe> japh!

    cheers

    tachyon

Re: case insensitive matching
by Fletch (Bishop) on Feb 14, 2004 at 21:45 UTC

    Out on a limb: another posibility is that this isn't really perl (but you're asking here anyway because of the proliferation of things promising "Perl Compatible Regexes" which really aren't) and you're using something with a regex variant where \< and \> are actually more specific versions of what would be \b in perl (specifying beginning and end of word respectively).