Regex: ignore case sensitivity
RegexRegex Problem Overview
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].*
Regex Solutions
Solution 1 - Regex
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:
-
Use the
(?i)
and [optionally](?-i)
mode modifiers:(?i)G[a-b](?-i).*
-
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.
Solution 2 - Regex
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
Solution 3 - Regex
regular expression for validate 'abc' ignoring case sensitive
(?i)(abc)
Solution 4 - Regex
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].*/
.
Solution 5 - Regex
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))
{
...
}
Solution 6 - Regex
As I discovered from this similar post (https://stackoverflow.com/questions/5228892/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.
Solution 7 - Regex
C#
using System.Text.RegularExpressions;
...
Regex.Match(
input: "Check This String",
pattern: "Regex Pattern",
options: RegexOptions.IgnoreCase)
specifically: options: RegexOptions.IgnoreCase
Solution 8 - Regex
[gG][aAbB].* probably simples solution if the pattern is not too complicated or long.
Solution 9 - Regex
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
Solution 10 - Regex
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"]
Solution 11 - Regex
Addition to the already-accepted answers:
Grep usage:
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.
Also, here's a great tool for verifying regular expressions: https://regex101.com/
Ex: See the expression and Explanation in this image.
References:
-
man pages (
man grep
) -
http://droptips.com/using-grep-and-ignoring-case-case-insensitive-grep
Solution 12 - Regex
In Java, Regex
constructor has
Regex(String pattern, RegexOption option)
So to ignore cases, use
option = RegexOption.IGNORE_CASE
Solution 13 - Regex
Kotlin:
"G[a-b].*".toRegex(RegexOption.IGNORE_CASE)
Solution 14 - Regex
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 .
Solution 15 - Regex
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