Regular Expression Modifiers in Python

PythonServer Side ProgrammingProgramming

Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these −

Sr.No.Modifier & Description
1re.I
Performs case-insensitive matching.
2re.L
Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior(\b and \B).
3re.M
Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).
4re.S
Makes a period (dot) match any character, including a newline.
5re.U
Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
6re.X
Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.
raja
Published on 30-Jan-2020 12:05:43
Advertisements