Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match a string in incorrect case

Tags:

regex

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.

like image 449
Joe Daley Avatar asked Dec 01 '25 19:12

Joe Daley


2 Answers

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)/
like image 147
Casimir et Hippolyte Avatar answered Dec 03 '25 14:12

Casimir et Hippolyte


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]/
like image 35
perreal Avatar answered Dec 03 '25 12:12

perreal