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]
Some other articles
Y115 - Additional Python Facilities Some gems from Intermediate Python Command line parameter handling in Python via the argparse module Json load from URL, recursive display, Python 3.4 Running an operating system command from your Python program - the new way with the subprocess module Json is the new marshall, pickle and cPickle / Python Python - an interesting application Handling JSON in Python (and a csv, marshall and pickle comparison) JSON from Python - first principles, easy example Teaching dilemma - old tricks and techniques, or recent enhancements? A demonstration of how many Python facilities work together Python regular expressions - repeating, splitting, lookahead and lookbehind Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Running operating system commands from your Python program Python decorators - your own, staticmethod and classmethod Model - View - Controller demo, Sqlite - Python 3 - Qt4 Connecting Python to sqlite and MySQL databases Regular Expressions in Python Python - what is going on around me? Python - how it saves on compile time Serialization - storing and reloading objects Testing code in Python - doctest, unittest and others Python Regular Expressions A series of tyre damages This article Regular expressions made easy - building from components Turning objects into something you can store - Pickling (Python) Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP Sending an email from Python Python - listing out the contents of all variables Python 3000 - the next generation Keeping your regular expressions simple Python to MySQL Splitting the difference What and why for the epoch Examples - Gadfly, NI Number, and Tcl to C interface The elegance of Python T247 - Advanced Regular Expressions Regular Expression Substitution - Tcl Regular expression for 6 digits OR 25 digits Sparse and Greedy matching - Tcl 8.4 Tcl / regsub - changing a string and using interesting bits This article Regular expressions made easy - building from components Regular Express Primer Matching within multiline strings, and ignoring case in regular expressions Q803 - Regular Expressions - Extra Elements Possessive Regular Expression Matching - Perl, Objective C and some other languages Regular Expression modifiers in PHP - summary table Looking ahead and behind in Regular Expressions - double matching Python regular expressions - repeating, splitting, lookahead and lookbehind Be gentle rather than macho ... regular expression techniques Seven new intermediate Perl examples Finding words and work boundaries (MySQL, Perl, PHP) Regular expression for 6 digits OR 25 digits Replacing the last comma with an and A taster PHP expression ... This article Matching within multiline strings, and ignoring case in regular expressions P212 - More on Character Strings Binary data handling - Python and Perl First match or all matches? Perl Regular Expressions Converting codons via Amino Acids to Proteins in Perl Possessive Regular Expression Matching - Perl, Objective C and some other languages Serialsing and unserialising data for storage and transfer in Perl The difference between dot (a.k.a. full stop, period) and comma in Perl Single and double quotes strings in Perl - what is the difference? DNA to Amino Acid - a sample Perl script How much has Perl (and other languages) changed? Looking ahead and behind in Regular Expressions - double matching Object Orientation in an hour and other Perl Lectures Arrays v Lists - what is the difference, why use one or the other Further more advanced Perl examples Unpacking a Perl string into a list Teaching examples in Perl - third and final part Binary data handling with unpack in Perl Want to do a big batch edit? Nothing beats Perl! Making variables persistant, pretending a database is a variable and other Perl tricks Running a piece of code is like drinking a pint of beer Perl substitute - the e modifier Finding words and work boundaries (MySQL, Perl, PHP) Equality and looks like tests - Perl Handling Binary data (.gif file example) in Perl This article Regular expressions made easy - building from components Substitute operator / modifiers in Perl Commenting a Perl Regular Expression Perl, the substitute operator s Matching within multiline strings, and ignoring case in regular expressions C++ and Perl - why did they do it THAT way? Coloured text in a terminal from Perl Don't expose your regular expressions Storing a regular expression in a perl variable Perl Regular Expressions - finding the position and length of the match Remember to process blank lines Commenting Perl regular expressions H107 - String Handling in PHP Splitting the difference with PHP Setting up strings in PHP Solution looking for a problem? Lookahead and Lookbehind More than just matching with a regular expression in PHP Getting more than a yes / no answer from a regular expression pattern match Learning to program in PHP - Regular Expression and Associative Array examples Regular Expression modifiers in PHP - summary table PHP - moving from ereg to preg for regular expressions Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Handling (expanding) tabs in PHP Curly braces within double quoted strings in PHP Handling nasty characters - Perl, PHP, Python, Tcl, Lua Making Regular Expressions easy to read and maintain Finding variations on a surname Regular Expressions in PHP Regular expression for 6 digits OR 25 digits Do not SHOUT and do not whisper Short and sweet and sticky - PHP form input A taster PHP expression ... This article Regular Express Primer PHP Regular expression to extrtact link and text Date conversion - PHP Looking ahead and behind in a Regular Expression Evaluating arithmetic expressions in configuration files How similar are two words Don't expose your regular expressions Robust PHP user inputs PHP - dividing a string up into pieces The fencepost problem Converting between acres and hectares Running a Perl script within a PHP page Splitting the difference PHP Magic Quotes the array returned by preg_match_all PHP and natural sorting Here documents