2

How do I write a regular expression that matches a particular string in all combinations of upper and lower case letters except for one?

For example, take the string "SuperMario". What regular expression matches that string in all other combinations of upper and lower case letters?

The regular expression should match:

  • sUPERmARIO
  • Supermario

The regular expression should not match:

  • SuperMario
  • Supermari

Perl compatible regular expression preferred.

Joe Daley
  • 45,356
  • 15
  • 65
  • 64

3 Answers3

10

You can use this:

/(?!SuperMario)(?i)supermario/

EDIT:

Note that you will have better performances with a lookbehind, if your string contains other things:

/(?i)supermario(?<!(?-i)SuperMario)/
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
3
my $s = "Supermario";
if ($s =~ /supermario/i and $s !~ /SuperMario/) {
    print "wrong\n";
}

Another method:

/(?:[S](?!uperMario)|s)[Uu][Pp][eE][rR][mM][aA][Rr][iI][oO]/
perreal
  • 94,503
  • 21
  • 155
  • 181
0

My Perl is rusty, and this is not using regex, but how about:

my $term = "SuperMario";
my $input = "SuperMario";
if ( $input ne $term && uc($input) eq uc($term) ){
    print "match";
}
cbayram
  • 2,259
  • 11
  • 9