Regex: ignore case sensitivity
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].*
Solution 1:
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:
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:
regular expression for validate 'abc' ignoring case sensitive
(?i)(abc)
Solution 4:
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].*/
.