3/31/2023 0 Comments Regex ignore case![]() metacharacter to match newline character flags=re.S|re.I multiple flags can be combined using the | operator re.MULTILINE or re.M allow ^ and $ anchors to match line wise re.VERBOSE or re.X allows to use literal whitespaces for aligning purposes and to add comments after the # character escape spaces and # if needed as part of actual RE (?#comment) another way to add comments, not a flag (?flags:pat) inline flags only for this pat, overrides flags argument where flags is i for re.I, s for re.S, etc except L for re.L (?-flags:pat) negate flags only for this pat (?flags-flags:pat) apply and negate particular flags only for this pat (?flags) apply flags for whole RE, can be used only at start of RE anchors if any, should be specified after (?flags) ![]() Note Description re.IGNORECASE or re.I flag to ignore case re.DOTALL or re.S allow. > re.findall(r'(?-i:Cat)*\b', 'Cat SCatTeR CATER cAts', flags=re.I) # case-sensitive only for the 'Cat' portion > re.findall(r'(?i)Cat*\b', 'Cat SCatTeR CATER cAts') # case-insensitive for the whole RE definition using inline flags > re.findall(r'Cat*\b', 'Cat SCatTeR CATER cAts', flags=re.I) # case-insensitive for the whole RE definition using flags argument > re.findall(r'Cat(?i:*)\b', 'Cat SCatTeR CATER cAts') # case-insensitive only for the '*' portion > re.findall(r'Cat*\b', 'Cat SCatTeR CATER cAts') # case-sensitive for the whole RE definition And as can be observed from the below examples, these do not act as capture groups. For example, i for re.I, s for re.S and so on, except L for re.L or re.LOCALE (discussed in the re.ASCII section). The flags are to be given as single letter lowercase version of short form constants. In these ways, flags can be specified precisely only where it is needed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |