121

I've seen lots of examples of making an entire regular expression case-insensitive. What I'm wondering about is having just part of the expression be case-insensitive.

For example, let's say I have a string like this:

fooFOOfOoFoOBARBARbarbarbAr

What if I want to match all occurrences of "foo" regardless of case but I only want to match the upper-case "BAR"s?

The ideal solution would be something that works across regex flavors but I'm interested in hearing language-specific ones as well (Thanks Espo)

Edit

The link Espo provided was very helpful. There's a good example in there about turning modifiers on and off within the expression.

For my contrived example, I can do something like this:

(?i)foo*(?-i)|BAR

which makes the match case-insensitive for just the foo portion of the match.

That seemed to work in most regex implementations except Javascript, Python, and a few others (as Espo mentioned).

The big ones that I was wondering about (Perl, PHP, .NET) all support inline mode changes.

1

5 Answers 5

98

Perl lets you make part of your regular expression case-insensitive by using the (?i:) pattern modifier.

Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the right of the modifier. You can turn off modes by preceding them with a minus sign. All modes after the minus sign will be turned off. E.g. (?i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.

Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don't support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.

You can quickly test how the regex flavor you're using handles mode modifiers. The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.

Source

0
24

It is true one can rely on inline modifiers as described in Turning Modes On and Off for Only Part of The Regular Expression:

The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.

However, a bit more supported feature is an (?i:...) inline modifier group (see Modifier Spans). The syntax is (?i:, then the pattern that you want to make cas-insensitive, and then a ).

(?i:foo)|BAR

The reverse: If your pattern is compiled with a case insensitive option and you need to make a part of a regex case sensitive, you add - after ?: (?-i:...).

Example uses in various languages (wrapping the matches with angle brackets):

  • - preg_replace("~(?i:foo)|BAR~", '<$0>', "fooFOOfOoFoOBARBARbarbarbAr") (demo)
  • - re.sub(r'(?i:foo)|BAR', r'<\g<0>>', 'fooFOOfOoFoOBARBARbarbarbAr') (demo) (note Python re supports inline modifier groups since Python 3.6)
  • / / - Regex.Replace("fooFOOfOoFoOBARBARbarbarbAr", "(?i:foo)|BAR", "<$&>") (demo)
  • - "fooFOOfOoFoOBARBARbarbarbAr".replaceAll("(?i:foo)|BAR", "<$0>") (demo)
  • - $s =~ s/(?i:foo)|BAR/<$&>/g (demo)
  • - "fooFOOfOoFoOBARBARbarbarbAr".gsub(/(?i:foo)|BAR/, '<\0>') (demo)
  • - gsub("((?i:foo)|BAR)", "<\\1>", "fooFOOfOoFoOBARBARbarbarbAr", perl=TRUE) (demo)
  • - "fooFOOfOoFoOBARBARbarbarbAr".replacingOccurrences(of: "(?i:foo)|BAR", with: "<$0>", options: [.regularExpression])
  • - (uses RE2) - regexp.MustCompile(`(?i:foo)|BAR`).ReplaceAllString( "fooFOOfOoFoOBARBARbarbarbAr", `<${0}>`) (demo)

Not supported in , , , std::regex, , .

In these case, you can put both letter variants into a character class (not a group, see Why is a character class faster than alternation?). Examples:

  • - sed -E 's/[Ff][Oo][Oo]|BAR/<&>/g' file > outfile (demo)
  • - grep -Eo '[Ff][Oo][Oo]|BAR' file (or if you are using GNU grep, you can still use the PCRE regex, grep -Po '(?i:foo)|BAR' file (demo))
2
  • 1
    It is supported in bash, if using "Perl-like" regex. Try: echo BAR | grep -P '(?i)bar'
    – Noam Manos
    Sep 14, 2020 at 7:50
  • 1
    @NoamManos That is not pure Bash, that is grep, and it is already related to PCRE, which is Perl-compatible regular expression library, so it is already coverd in perl. As you see, I did not list grep here, since grep -P option is not universally supported (it is only supported by GNU grep) Sep 14, 2020 at 9:13
6

What language are you using? A standard way to do this would be something like /([Ff][Oo]{2}|BAR)/ with case sensitivity on, but in Java, for example, there is a case sensitivity modifier (?i) which makes all characters to the right of it case insensitive and (?-i) which forces sensitivity. An example of that Java regex modifier can be found here.

3
  • +1 Why bother making it case insensitive when you can match both cases
    – Nona Urbiz
    Sep 15, 2009 at 18:30
  • 15
    @NonaUrbiz: Because the expression (?i)foobar is more readable than [Ff][Oo]{2}[Bb][Aa][Rr]
    – Thanatos
    Oct 25, 2012 at 0:29
  • 2
    And because it can grow way much more hairy and complex.
    – Chop
    Feb 20, 2015 at 12:32
6

Unfortunately syntax for case-insensitive matching is not common. In .NET you can use RegexOptions.IgnoreCase flag or ?i modifier

4

You could use

(?:F|f)(?:O|o)(?:O|o)

The ?: in the brackets in .Net means it's non-capturing, and just used to group the terms of the | (or) statement.

1
  • 28
    Isn't "[fF][oO][oO]" the better alternative? For the example at hand you could even go as far as "[fF][oO]\{2}" ;-)
    – Tomalak
    Dec 11, 2008 at 20:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.