Python3 Reg Expressions
## Python3.x Python3 Regular Expressions\n\nA regular expression is a special sequence of characters that helps you easily check whether a string matches a certain pattern.\n\nIn Python, the re module is used to handle regular expressions.\n\nThe re module provides a set of functions that allow you to perform pattern matching, searching, and replacement operations in strings.\n\nThe re module gives Python complete regular expression functionality.\n\nThis chapter mainly introduces commonly used regular expression processing functions in Python. If you are not familiar with regular expressions, you can check our (#).\n\n* * *\n\n## re.match Function\n\nre.match attempts to match a pattern from the beginning of the string. If the match is not successful at the starting position, match() returns None.\n\n**Function Syntax**:\n\nre.match(pattern, string, flags=0)\nFunction Parameter Description:\n\n| Parameter | Description |\n| --- | --- |\n| pattern | Regular expression to match |\n| string | String to be matched |\n| flags | Flags for controlling regular expression matching behavior, such as: case sensitivity, multiline matching, etc. See: (#) |\n\nOn successful match, the re.match method returns a match object; otherwise, returns **None**.\n\nWe can use the group(num) or groups() match object functions to get the matching expression.\n\n| Match Object Method | Description |\n| --- | --- |\n| group(num=0) | The entire matched expression string. group() can accept multiple group numbers at once, in which case it returns a tuple containing the values corresponding to those groups. |\n| groups() | Returns a tuple containing all group strings, from 1 to the number of groups contained. |\n\n## Examples\n\nimport re print(re.match('www', 'www.').span())print(re.match('com', 'www.'))\n\nThe above example outputs:\n\n(0, 3)None\n\n## Example\n\nimport re line = "Cats are smarter than dogs"matchObj = re.match(r'(.*) are (.*?) .*', line, re.M|re.I)if matchObj: print("matchObj.group() : ", matchObj.group())print("matchObj.group(1) : ", matchObj.group(1))print("matchObj.group(2) : ", matchObj.group(2))else: print("No match!!")\n\nThe execution result of the above example is as follows:\n\nmatchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter\n\n* * *\n\n## re.search Method\n\nre.search scans the entire string and returns the first successful match.\n\nFunction Syntax:\n\nre.search(pattern, string, flags=0)\nFunction Parameter Description:\n\n| Parameter | Description |\n| --- | --- |\n| pattern | Regular expression to match |\n| string | String to be matched |\n| flags | Flags for controlling regular expression matching behavior, such as: case sensitivity, multiline matching, etc. See: (#) |\n\nOn successful match, re.search method returns a match object; otherwise, returns None.\n\nWe can use group(num) or groups() match object functions to get the matching expression.\n\n| Match Object Method | Description |\n| --- | --- |\n| group(num=0) | The entire matched expression string. group() can accept multiple group numbers at once, in which case it returns a tuple containing the values corresponding to those groups. |\n| groups() | Returns a tuple containing all group strings, from 1 to the number of groups contained. |\n\n## Example\n\nimport re print(re.search('www', 'www.').span())print(re.search('com', 'www.').span())\n\nThe above example outputs:\n\n(0, 3)(11, 14)\n\n## Example\n\nimport re line = "Cats are smarter than dogs"searchObj = re.search(r'(.*) are (.*?) .*', line, re.M|re.I)if searchObj: print("searchObj.group() : ", searchObj.group())print("searchObj.group(1) : ", searchObj.group(1))print("searchObj.group(2) : ", searchObj.group(2))else: print("Nothing found!!")\n\nThe execution result of the above example is as follows:\n\nsearchObj.group() : Cats are smarter than dogs searchObj.group(1) : Cats searchObj.group(2) : smarter\n\n* * *\n\n## Difference Between re.match and re.search\n\nre.match only matches the beginning of the string. If the string beginning does not match the regular expression, the match fails and the function returns None, while re.search matches the entire string until it finds a match.\n\n## Example\n\nimport re line = "Cats are smarter than dogs"matchObj = re.match(r'dogs', line, re.M|re.I)if matchObj: print("match --> matchObj.group() : ", matchObj.group())else: print("No match!!")matchObj = re.search(r'dogs', line, re.M|re.I)if matchObj: print("search --> matchObj.group() : ", matchObj.group())else: print("No match!!")\n\nThe execution result of the above example is as follows:\n\nNo match!! search --> matchObj.group() : dogs\n\n* * *\n\n## Search and Replace\n\nPython's re module provides re.sub for replacing matched items in a string.\n\nSyntax:\n\nre.sub(pattern, repl, string, count=0, flags=0)\nParameters:\n\n* pattern: Pattern string in the regular expression.\n* repl: Replacement string, can also be a function.\n* string: Original string to be searched and replaced.\n* count: Maximum number of replacements after pattern matching. Default 0 means replace all matches.\n* flags: Matching mode used during compilation, in numeric form.\n\nThe first three parameters are required, and the last two are optional.\n\n## Example\n\nimport re phone = "2004-959-559 # This is a phone number"num = re.sub(r'#.*$', "", phone)print("Phone number : ", num)num = re.sub(r'D', "", phone)print("Phone number : ", num)\n\nThe execution result of the above example is as follows:\n\nPhone number : 2004-959-559 Phone number : 2004959559\n### repl Parameter is a Function\n\nThe following example multiplies the matched numbers in the string by 2:\n\n## Example\n\nimport re def double(matched): value = int(matched.group('value'))return str(value * 2)s = 'A23G4HFD567'print(re.sub('(?Pd+)', double, s))\n\nThe execution output is:\n\nA46G8HFD1134\n### compile Function\n\nThe compile function is used to compile a regular expression, generating a regular expression (Pattern) object for use by the match() and search() functions.\n\nSyntax format:\n\nre.compile(pattern[, flags])\nParameters:\n\n* pattern: A regular expression in string form\n* flags Optional, represents the matching mode, such as case-insensitive, multiline mode, etc. The specific parameters are:\n* **re.IGNORECASE or re.I**\n - Makes matching case-insensitive * re.L Represents special character sets w, W, b, B, s, S dependent on the current environment\n * re.MULTILINE or re.M - Multiline mode, changes the behavior of ^ and $ to match the beginning and end of each line in the string.\n * re.DOTALL or re.S - Makes . match any character including newline.\n * re.ASCII - Makes w, W, b, B, d, D, s, S match only ASCII characters.\n * re.VERBOSE or re.X - Ignores whitespace and comments, allowing for clearer organization of complex regular expressions.\n\nThese flags can be used individually or combined using bitwise OR (|). For example, re.IGNORECASE | re.MULTILINE enables both case-insensitive and multiline modes simultaneously.\n\n### Example\n\n## Example\n\n>>>import re>>>pattern = re.compile(r'd+')>>>m = pattern.match('one12twothree34four')>>>print(m)None>>>m = pattern.match('one12twothree34four', 2, 10)>>>print(m)None>>>m = pattern.match('one12twothree34four', 3, 10)>>>print(m)>>>m.group(0)'12'>>>m.start(0)3>>>m.end(0)5>>>m.span(0)(3, 5)\n\nIn the above, when matching succeeds, a Match object is returned, where:\n\n* The `group([group1, β¦])`
YouTip