Case Insensitive Regex Without Using Regexoptions Enumeration

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 insensitive
  • s, single line mode
  • m, multi line mode
  • x, 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:

  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.

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



Leave a reply



Submit