Main Content

Ignore case in Regular Expression

Archive - Originally posted on "The Horse's Mouth" - 2007-09-08 01:23:52 - Graham Ellis

Do you want to ignore case in a regular expression? There are a variety of ways of doing it ... depending on the language you're writing. Here are some hints:

/abcd/i Perl - an i after the regular expression
eregi PHP - use eregi rather than ereg
re.I or re.IGNORECASE Python - extra parameters to the compile method
-nocase Tcl - this option is available on regexes in Tcl

But there are times yo want to apply the "ignore case" to only part of a regular expression, or use it in some other circumstances such as within Tcl's array names command. And here you can use the embedded notation of the form (?i). Here's an example - in Tcl, but this technique works for most languages these days!

set results [array names table -regexp (?i)$where]