Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How can i make the following regex ignore case sensitive, and match all with the correct chars, but ignore wheter it is lower or uppercase?

G[a-b].*
share|improve this question
    
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
    
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
2  
If you're using Java, you can specify this with the Pattern class: Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);. – james.garriss Aug 6 '14 at 14:06
    
More Java options here: blogs.oracle.com/xuemingshen/entry/… – james.garriss Aug 6 '14 at 14:08
up vote 103 down vote accepted

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.

share|improve this answer

Depends on implementation but I would use

(?i)G[a-b].
share|improve this answer
    
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 5 '15 at 0:58

The i flag is normally for case sensitivity. You don't give a language here, but it'll probably be something like /G[a-b].*/i.

share|improve this answer

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))
{
...
}
share|improve this answer
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. – Frankenstein Aug 7 '13 at 6:24

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 .

share|improve this answer

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.

share|improve this answer

regular expression for validate 'abc' ignoring case sensitive

(?i)(abc)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.