543

How can I make the following regex ignore case sensitivity? It should match all the correct characters but ignore whether they are lower or uppercase.

G[a-b].*
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
brother
  • 7,651
  • 9
  • 34
  • 58
  • Just have both the uppercase and lowercase included in the regex or convert to uppercase before doing the regex matching – Chetter Hummin Mar 11 '12 at 13:05
  • 2
    G[a-bA-B].* would be the obvious in this general case, case sensitivity is afaik platform dependent and you're not giving a platform. – Joachim Isaksson Mar 11 '12 at 13:07
  • 25
    If you're using Java, you can specify this with the Pattern class: `Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);`. – james.garriss Aug 06 '14 at 14:06
  • More Java options here: https://blogs.oracle.com/xuemingshen/entry/case_insensitive_matching_in_java – james.garriss Aug 06 '14 at 14:08
  • Note that for `grep`ing it is simply the addition of the `-i` modifier. Ex: `grep -rni regular_expression` to search for this 'regular_expression' 'r'ecursively, case 'i'nsensitive, showing line 'n'umbers in the result. – Gabriel Staples Oct 17 '18 at 22:24
  • @JoachimIsaksson Correction: `[Gg][abAB].*` Your regex would have missed `gable` as it insisted on a capital `G`. – Luv2code Nov 26 '19 at 13:21

15 Answers15

672

Assuming you want the whole regex to ignore case, you should look for the i flag. Nearly all regex engines support it:

/G[a-b].*/i

string.match("G[a-b].*", "i")

Check the documentation for your language/platform/tool to find how the matching modes are specified.

If you want only part of the regex to be case insensitive (as my original answer presumed), then you have two options:

  1. Use the (?i) and [optionally] (?-i) mode modifiers:

    (?i)G[a-b](?-i).*
    
  2. Put all the variations (i.e. lowercase and uppercase) in the regex - useful if mode modifiers are not supported:

    [gG][a-bA-B].*
    

One last note: if you're dealing with Unicode characters besides ASCII, check whether or not your regex engine properly supports them.

mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • Brilliant! Works for: perl -pe 's/^utf-8\?B\?.*$//gi' Cancer.1631, matching/replacing string "UTF-8?B?" in file Cancer.1631. This fails: perl -pe 's/^utf-8\?B\?.*$//g' Cancer.1631, due to the case mismatch. – Victoria Stuart Jan 18 '18 at 01:26
  • 8
    This post would much clearer if it wasn't such a specific example. Like what if you want to ignore case for another word such as ".txt" and ".TXT". From looking at this answer I'm still unsure how I could do this. – Kyle Bridenstine Jun 12 '18 at 23:14
  • For some reason the regex that you posted doesn't work in `find` extended regex.. for example `find . \( -type d -regextype posix-extended -regex '/./[a-c][^.]*/i' \)` doesn't show any folders.. white a similar reged `find . \( -type d -regextype posix-extended -regex './[a-c][^.]*' \)` without the modifiers does show the correct folders. Any idea why? – alpha_989 Jul 01 '18 at 21:19
  • Here I am trying to find all the folders starting with characters `[a-c]` in the current folder only.. to do some more manipulation.. – alpha_989 Jul 01 '18 at 21:20
  • Honestly I'd put option 2 in the main part of the answer as it is generic and works with all regex engines. – Puterdo Borato Nov 21 '19 at 14:53
  • For Dart, the `RegExp` class has a parameter for it `RegExp(String source, {bool multiLine = false, bool caseSensitive = true,` – ThinkDigital Jun 26 '20 at 23:51
288

Depends on implementation but I would use

(?i)G[a-b].

VARIATIONS:

(?i) case-insensitive mode ON    
(?-i) case-insensitive mode OFF

Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?im) in the middle of the regex then the modifier only applies to the part of the regex to the right of the modifier. With these flavors, you can turn off modes by preceding them with a minus sign (?-i).

Description is from the page: https://www.regular-expressions.info/modifiers.html

elrado
  • 4,960
  • 1
  • 17
  • 15
  • This is the modifier format for TortoiseHg's Search regex engine. – mwolfe02 Mar 12 '15 at 14:08
  • Could you tell me how this can be achieved in Linux shell (say in egrep without using the "-i" switch) generically? – Krishna Gupta Sep 05 '15 at 00:58
  • 3
    Explaining what the `(?i)` does and how to end it (`(?-i)`) would have been really helpful. That's hands-down why your answer has 1/3 as many votes as the #1 question instead of almost as many, as they explain this subtle detail. – Gabriel Staples Oct 17 '18 at 22:13
  • For who is using https://pypi.org/project/regex/, using (?i) in a regex which belongs to a regex composition (i.e. join of regex with ORs) will give to it an higher priority with respect the others (i.e. the order will be no more the same ad the regex are written) – albero Apr 28 '21 at 15:15
  • Works for me in .NET/C#. – Justin Harris Aug 16 '21 at 17:51
  • It is not about the language, but the regex engine implementation. But yes great to hear it is not something limited to perl :D. – elrado Aug 17 '21 at 06:58
100

regular expression for validate 'abc' ignoring case sensitive

(?i)(abc)
Ravinath
  • 1,620
  • 1
  • 14
  • 8
58

The i flag is normally used for case insensitivity. You don't give a language here, but it'll probably be something like /G[ab].*/i or /(?i)G[ab].*/.

PoolloverNathan
  • 148
  • 2
  • 11
chooban
  • 9,018
  • 2
  • 20
  • 36
17

Just for the sake of completeness I wanted to add the solution for regular expressions in C++ with Unicode:

std::tr1::wregex pattern(szPattern, std::tr1::regex_constants::icase);

if (std::tr1::regex_match(szString, pattern))
{
...
}
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Holger Schmeken
  • 605
  • 6
  • 16
  • 1
    Can someone clarify to me why this post was downvoted? The accepted solution uses specific code and for the sake of completeness I wanted to add the solution for the standard libraries of the language c++. In my opinion I have generated added value to a more general question. – Holger Schmeken Aug 07 '13 at 06:24
  • szPattern is OP's `G[a-b].*` whereas szString is the string to test – jlaurens Oct 21 '21 at 07:20
7

JavaScript

If you want to make it case insensitive just add i at the end of regex:

'Test'.match(/[A-Z]/gi) //Returns ["T", "e", "s", "t"]

Without i

'Test'.match(/[A-Z]/g) //Returns ["T"]

Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20
7

In JavaScript you should pass the i flag to the RegExp constructor as stated in MDN:

const regex = new RegExp('(abc)', 'i');

regex.test('ABc'); // true
Yulian
  • 6,262
  • 10
  • 65
  • 92
5

As I discovered from this similar post (ignorecase in AWK), on old versions of awk (such as on vanilla Mac OS X), you may need to use 'tolower($0) ~ /pattern/'.

IGNORECASE or (?i) or /pattern/i will either generate an error or return true for every line.

Community
  • 1
  • 1
senortim
  • 111
  • 1
  • 6
5

C#

using System.Text.RegularExpressions;
...    
Regex.Match(
    input: "Check This String",
    pattern: "Regex Pattern",
    options: RegexOptions.IgnoreCase)

specifically: options: RegexOptions.IgnoreCase

DonkeyKong
  • 1,005
  • 14
  • 18
3

[gG][aAbB].* probably simples solution if the pattern is not too complicated or long.

alpha_989
  • 4,882
  • 2
  • 37
  • 48
1

Addition to the already-accepted answers:

Grep usage:

Note that for greping it is simply the addition of the -i modifier. Ex: grep -rni regular_expression to search for this 'regular_expression' 'r'ecursively, case 'i'nsensitive, showing line 'n'umbers in the result.

Also, here's a great tool for verifying regular expressions: https://regex101.com/

Ex: See the expression and Explanation in this image.

enter image description here

References:

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
1

In Java, Regex constructor has

Regex(String pattern, RegexOption option)

So to ignore cases, use

option = RegexOption.IGNORE_CASE
Aziz
  • 1,976
  • 20
  • 23
1

Kotlin:

"G[a-b].*".toRegex(RegexOption.IGNORE_CASE)
Blundell
  • 75,855
  • 30
  • 208
  • 233
-3

You also can lead your initial string, which you are going to check for pattern matching, to lower case. And using in your pattern lower case symbols respectively .

Alexander Drobyshevsky
  • 3,907
  • 2
  • 20
  • 17
-4

You can practice Regex In Visual Studio and Visual Studio Code using find/replace.

You need to select both Match Case and Regular Expressions for regex expressions with case. Else [A-Z] won't work.enter image description here

Visual Studio 2019 Community

David Morrow
  • 262
  • 4
  • 9