Case insensitive Regex without using RegexOptions enumeration
MSDN Documentation
(?i)taylor
matches all of the inputs I specified without having to set the RegexOptions.IgnoreCase flag.
To force case sensitivity I can do (?-i)taylor
.
It looks like other options include:
i
, case insensitives
, single line modem
, multi line modex
, free spacing mode
How to make a regex match case insensitive?
Just use the option IgnoreCase
, see .NET regular Expression Options
So your regex creation could look like this
Regex r = new Regex(@"^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$", RegexOptions.IgnoreCase);
I removed also all your {1}
because it is superfluous. Every item is per default matched once, no need to state this explicitly.
The other possibility would be to use inline modifiers, when you are not able to set it on the object.
^(?i)[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$
C# Regex to match a Case Insensitive string that doesn't contain a certain string
Use RegexOptions.IgnoreCase:
Regex.Matches( text, @"^(?!.*None).*$", RegexOptions.IgnoreCase );
Regex.IsMatch( text, @"^(?!.*None).*$" , RegexOptions.IgnoreCase );
How to make a regex match case insensitive?
Just use the option IgnoreCase
, see .NET regular Expression Options
So your regex creation could look like this
Regex r = new Regex(@"^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$", RegexOptions.IgnoreCase);
I removed also all your {1}
because it is superfluous. Every item is per default matched once, no need to state this explicitly.
The other possibility would be to use inline modifiers, when you are not able to set it on the object.
^(?i)[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$
Regex: ignore case sensitivity
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.
How to make this regular expression case insensitive without RegexOptions.IgnoreCase?
It ain't pretty but if that's what you need :
^\b([mM][aA][lL][eE]|[fF][eE][mM][aA][lL][eE])\b$
Update 1
The word boundaries are not needed as noted in the comments
^([mM][aA][lL][eE]|[fF][eE][mM][aA][lL][eE])$
case insensitive regex
Instead of just [A-Z], use [A-Za-z].
But watch out: there are e-mail addresses that end in top-level domains like .travel, that are forbidden according to your regex!
Regular expression list matching without case sensitivity
You can use the i
(case insensitive) modifier like so:
pattern = /^jan*$/i; // <-- it goes at the end
Another way to define regular expressions is with the RegExp
object:
pattern = new RegExp("^jan*$", "i");
I find this form to be more readable.
Also keep in mind that /^jan*$/i
will match things like:
JAN
jannnn
jannNN
jAn
I'm not sure if that is what you want.
If you just want to match a predefined set you could opt for a non-regular-expression solution:
function isMonth(value) {
var months = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec".split("|");
return months.indexOf(value.toLowerCase()) !== -1;
}
Related Topics
Does the Order of Linq Functions Matter
ASP.NET Dynamically Button with Event Handler
Unit Testing That Events Are Raised in C# (In Order)
Generate Class from Database Table
How to Use C# 6 with Web Site Project Type
Async/Await - Is It *Concurrent*
Contextswitchdeadlock Was Detected Error in C#
Set Content-Type of Media Files Stored on Blob
C#: How to Convert a List of Objects to a List of a Single Property of That Object
String.Format - How It Works and How to Implement Custom Formatstrings
What's Wrong with This Division
Invalid Cast from 'System.Int32' to 'System.Nullable'1[[System.Int32, Mscorlib]]
In What Areas Might the Use of F# Be More Appropriate Than C#
Is There a Faster Way Than This to Find All the Files in a Directory and All Sub Directories
Intelligent Way of Removing Items from a List<T> While Enumerating in C#
Error Message "Cs5001 Program Does Not Contain a Static 'Main' Method Suitable for an Entry Point"
Can Connect to Ftp Using Filezilla or Winscp, But Not with Ftpwebrequest or Fluentftp