user1876508 user1876508 - 7 months ago 19
Python Question

How to use regular expressions in python to search for all capitalization combinations

In python, how can I make a regular expression that would match all capitalization combinations of a word, without formatting the data before hand. For example, let's say I want to match lines in a file that match the word name, however it is capitalized. That is, name could look like any of the following

Name
nAMe
NAme


etc.

Answer

Make sure to pass the re.IGNORECASE option when calling your match, find or search.

For your example it'd be something like:

import re
re.search('name', 'Name', re.IGNORECASE)
re.search('name', 'nAMe', re.IGNORECASE)
re.search('name', 'NAme', re.IGNORECASE)
Comments