{"id":951,"date":"2019-09-23T10:09:35","date_gmt":"2019-09-23T09:09:35","guid":{"rendered":"http:\/\/learntowish.com\/?p=951"},"modified":"2019-09-23T10:09:35","modified_gmt":"2019-09-23T09:09:35","slug":"regular-expression-in-python","status":"publish","type":"post","link":"https:\/\/learnscripting.org\/regular-expression-in-python\/","title":{"rendered":"Regular Expression In Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The module defines several functions, constants, and an exception. Some of the functions are simplified versions of the full-featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.compile<\/code>(<em>pattern<\/em>,\u00a0<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compile a regular expression pattern into a regular expression object, which can be used for matching using its&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.RegexObject.match\"><code>match()<\/code><\/a>&nbsp;and&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.RegexObject.search\"><code>search()<\/code><\/a>&nbsp;methods, described below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The expression\u2019s behavior can be modified by specifying a\u00a0<em>flags<\/em>\u00a0value. Values can be any of the following variables, combined using bitwise OR (the\u00a0<code>|<\/code>\u00a0operator).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sequence<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">prog = re.compile(pattern)\nresult = prog.match(string)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">is equivalent to<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = re.match(pattern, string)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">but using&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.compile\"><code>re.compile()<\/code><\/a>&nbsp;and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The compiled versions of the most recent patterns passed to&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.match\"><code>re.match()<\/code><\/a>,&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.search\"><code>re.search()<\/code><\/a>&nbsp;or&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.compile\"><code>re.compile()<\/code><\/a>&nbsp;are cached, so programs that use only a few regular expressions at a time needn\u2019t worry about compiling regular expressions.<code>re.<\/code><code>DEBUG<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Display debug information about compiled expression.<code>re.<\/code><code>I<\/code><code>re.<\/code><code>IGNORECASE<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Perform case-insensitive matching; expressions like&nbsp;<code>[A-Z]<\/code>&nbsp;will match lowercase letters, too. This is not affected by the current locale. To get this effect on non-ASCII Unicode characters such as&nbsp;<code>\u00fc<\/code>&nbsp;and&nbsp;<code>\u00dc<\/code>, add the&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>&nbsp;flag.<code>re.<\/code><code>L<\/code><code>re.<\/code><code>LOCALE<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make&nbsp;<code>\\w<\/code>,&nbsp;<code>\\W<\/code>,&nbsp;<code>\\b<\/code>,&nbsp;<code>\\B<\/code>,&nbsp;<code>\\s<\/code>&nbsp;and&nbsp;<code>\\S<\/code>&nbsp;dependent on the current locale.<code>re.<\/code><code>M<\/code><code>re.<\/code><code>MULTILINE<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When specified, the pattern character&nbsp;<code>'^'<\/code>&nbsp;matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character&nbsp;<code>'$'<\/code>&nbsp;matches at the end of the string and at the end of each line (immediately preceding each newline). By default,&nbsp;<code>'^'<\/code>&nbsp;matches only at the beginning of the string, and&nbsp;<code>'$'<\/code>&nbsp;only at the end of the string and immediately before the newline (if any) at the end of the string.<code>re.<\/code><code>S<\/code><code>re.<\/code><code>DOTALL<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make the&nbsp;<code>'.'<\/code>&nbsp;special character match any character at all, including a newline; without this flag,&nbsp;<code>'.'<\/code>&nbsp;will match anything&nbsp;<em>except<\/em>&nbsp;a newline.<code>re.<\/code><code>U<\/code><code>re.<\/code><code>UNICODE<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make the&nbsp;<code>\\w<\/code>,&nbsp;<code>\\W<\/code>,&nbsp;<code>\\b<\/code>,&nbsp;<code>\\B<\/code>,&nbsp;<code>\\d<\/code>,&nbsp;<code>\\D<\/code>,&nbsp;<code>\\s<\/code>&nbsp;and&nbsp;<code>\\S<\/code>&nbsp;sequences dependent on the Unicode character properties database. Also enables non-ASCII matching for&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.IGNORECASE\"><code>IGNORECASE<\/code><\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>New in version 2.0.<\/em><code>re.<\/code><code>X<\/code><code>re.<\/code><code>VERBOSE<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class, or when preceded by an unescaped backslash, or within tokens like&nbsp;<code>*?<\/code>,&nbsp;<code>(?:<\/code>&nbsp;or&nbsp;<code>(?P&lt;...&gt;<\/code>. When a line contains a&nbsp;<code>#<\/code>&nbsp;that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such&nbsp;<code>#<\/code>&nbsp;through the end of the line are ignored.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This means that the two following regular expression objects that match a decimal number are functionally equal:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = re.compile(r\"\"\"\\d +  # the integral part\n                   \\.    # the decimal point\n                   \\d *  # some fractional digits\"\"\", re.X)\nb = re.compile(r\"\\d+\\.\\d*\")\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.<\/code><code>search<\/code>(<em>pattern<\/em>,&nbsp;<em>string<\/em>,&nbsp;<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scan through\u00a0<em>string<\/em>\u00a0looking for the first location where the regular expression\u00a0<em>pattern<\/em>\u00a0produces a match, and return a corresponding\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MatchObject\"><code>MatchObject<\/code><\/a>\u00a0instance. Return\u00a0<code>None<\/code>\u00a0if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.match<\/code>(<em>pattern<\/em>,\u00a0<em>string<\/em>,\u00a0<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If zero or more characters at the beginning of&nbsp;<em>string<\/em>&nbsp;match the regular expression&nbsp;<em>pattern<\/em>, return a corresponding&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MatchObject\"><code>MatchObject<\/code><\/a>&nbsp;instance. Return&nbsp;<code>None<\/code>&nbsp;if the string does not match the pattern; note that this is different from a zero-length match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that even in&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MULTILINE\"><code>MULTILINE<\/code><\/a>&nbsp;mode,&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.match\"><code>re.match()<\/code><\/a>&nbsp;will only match at the beginning of the string and not at the beginning of each line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to locate a match anywhere in\u00a0<em>string<\/em>, use\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.search\"><code>search()<\/code><\/a>\u00a0instead (see also\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#search-vs-match\">search() vs. match()<\/a>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.split<\/code>(<em>pattern<\/em>,\u00a0<em>string<\/em>,\u00a0<em>maxsplit=0<\/em>,\u00a0<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Split&nbsp;<em>string<\/em>&nbsp;by the occurrences of&nbsp;<em>pattern<\/em>. If capturing parentheses are used in&nbsp;<em>pattern<\/em>, then the text of all groups in the pattern are also returned as part of the resulting list. If&nbsp;<em>maxsplit<\/em>&nbsp;is nonzero, at most&nbsp;<em>maxsplit<\/em>&nbsp;splits occur, and the remainder of the string is returned as the final element of the list. (Incompatibility note: in the original Python 1.5 release,&nbsp;<em>maxsplit<\/em>&nbsp;was ignored. This has been fixed in later releases.)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>re.split('\\W+', 'Words, words, words.')\n['Words', 'words', 'words', '']\n<strong>&gt;&gt;&gt; <\/strong>re.split('(\\W+)', 'Words, words, words.')\n['Words', ', ', 'words', ', ', 'words', '.', '']\n<strong>&gt;&gt;&gt; <\/strong>re.split('\\W+', 'Words, words, words.', 1)\n['Words', 'words, words.']\n<strong>&gt;&gt;&gt; <\/strong>re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)\n['0', '3', '9']\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If there are capturing groups in the separator and it matches at the start of the string, the result will start with an empty string. The same holds for the end of the string:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>re.split('(\\W+)', '...words, words...')\n['', '...', 'words', ', ', 'words', '...', '']\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That way, separator components are always found at the same relative indices within the result list (e.g., if there\u2019s one capturing group in the separator, the 0th, the 2nd and so forth).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<em>split<\/em>&nbsp;will never split a string on an empty pattern match. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>re.split('x*', 'foo')\n['foo']\n<strong>&gt;&gt;&gt; <\/strong>re.split(\"(?m)^$\", \"foo<strong>\\n\\n<\/strong>bar<strong>\\n<\/strong>\")\n['foo\\n\\nbar\\n']\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Changed in version 2.7:\u00a0<\/em>Added the optional flags argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.findall<\/code>(<em>pattern<\/em>,\u00a0<em>string<\/em>,\u00a0<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Return all non-overlapping matches of&nbsp;<em>pattern<\/em>&nbsp;in&nbsp;<em>string<\/em>, as a list of strings. The&nbsp;<em>string<\/em>&nbsp;is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Due to the limitation of the current implementation the character following an empty match is not included in a next match, so&nbsp;<code>findall(r'^|\\w+',&nbsp;'two&nbsp;words')<\/code>&nbsp;returns&nbsp;<code>['',&nbsp;'wo',&nbsp;'words']<\/code>&nbsp;(note missed \u201ct\u201d). This is changed in Python 3.7.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>New in version 1.5.2.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Changed in version 2.4:\u00a0<\/em>Added the optional flags argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.finditer<\/code>(<em>pattern<\/em>,\u00a0<em>string<\/em>,\u00a0<em>flags=0<\/em>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Return an&nbsp;<a href=\"https:\/\/docs.python.org\/2\/glossary.html#term-iterator\">iterator<\/a>&nbsp;yielding&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MatchObject\"><code>MatchObject<\/code><\/a>&nbsp;instances over all non-overlapping matches for the RE&nbsp;<em>pattern<\/em>&nbsp;in&nbsp;<em>string<\/em>. The&nbsp;<em>string<\/em>&nbsp;is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. See also the note about&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.findall\"><code>findall()<\/code><\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>New in version 2.2.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Changed in version 2.4:&nbsp;<\/em>Added the optional flags argument.<code>re.<\/code><code>sub<\/code>(<em>pattern<\/em>,&nbsp;<em>repl<\/em>,&nbsp;<em>string<\/em>,&nbsp;<em>count=0<\/em>,&nbsp;<em>flags=0<\/em>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Return the string obtained by replacing the leftmost non-overlapping occurrences of&nbsp;<em>pattern<\/em>&nbsp;in&nbsp;<em>string<\/em>&nbsp;by the replacement&nbsp;<em>repl<\/em>. If the pattern isn\u2019t found,&nbsp;<em>string<\/em>&nbsp;is returned unchanged.&nbsp;<em>repl<\/em>&nbsp;can be a string or a function; if it is a string, any backslash escapes in it are processed. That is,&nbsp;<code>\\n<\/code>&nbsp;is converted to a single newline character,&nbsp;<code>\\r<\/code>&nbsp;is converted to a carriage return, and so forth. Unknown escapes such as&nbsp;<code>\\j<\/code>&nbsp;are left alone. Backreferences, such as&nbsp;<code>\\6<\/code>, are replaced with the substring matched by group 6 in the pattern. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>re.sub(r'def\\s+([a-zA-Z_][a-zA-Z_0-9]*)\\s*\\(\\s*\\):',\n<strong>... <\/strong>       r'static PyObject*\\npy_\\1(void)\\n{',\n<strong>... <\/strong>       'def myfunc():')\n'static PyObject*\\npy_myfunc(void)\\n{'\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If&nbsp;<em>repl<\/em>&nbsp;is a function, it is called for every non-overlapping occurrence of&nbsp;<em>pattern<\/em>. The function takes a single match object argument, and returns the replacement string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> dashrepl(matchobj):\n<strong>... <\/strong>    <strong>if<\/strong> matchobj.group(0) == '-': <strong>return<\/strong> ' '\n<strong>... <\/strong>    <strong>else<\/strong>: <strong>return<\/strong> '-'\n<strong>&gt;&gt;&gt; <\/strong>re.sub('-{1,2}', dashrepl, 'pro----gram-files')\n'pro--gram files'\n<strong>&gt;&gt;&gt; <\/strong>re.sub(r'\\sAND\\s', ' &amp; ', 'Baked Beans And Spam', flags=re.IGNORECASE)\n'Baked Beans &amp; Spam'\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The pattern may be a string or an RE object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The optional argument&nbsp;<em>count<\/em>&nbsp;is the maximum number of pattern occurrences to be replaced;&nbsp;<em>count<\/em>&nbsp;must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, so&nbsp;<code>sub('x*',&nbsp;'-',&nbsp;'abc')<\/code>&nbsp;returns&nbsp;<code>'-a-b-c-'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In string-type&nbsp;<em>repl<\/em>&nbsp;arguments, in addition to the character escapes and backreferences described above,&nbsp;<code>\\g&lt;name&gt;<\/code>&nbsp;will use the substring matched by the group named&nbsp;<code>name<\/code>, as defined by the&nbsp;<code>(?P&lt;name&gt;...)<\/code>&nbsp;syntax.&nbsp;<code>\\g&lt;number&gt;<\/code>&nbsp;uses the corresponding group number;&nbsp;<code>\\g&lt;2&gt;<\/code>&nbsp;is therefore equivalent to&nbsp;<code>\\2<\/code>, but isn\u2019t ambiguous in a replacement such as&nbsp;<code>\\g&lt;2&gt;0<\/code>.&nbsp;<code>\\20<\/code>&nbsp;would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character&nbsp;<code>'0'<\/code>. The backreference&nbsp;<code>\\g&lt;0&gt;<\/code>&nbsp;substitutes in the entire substring matched by the RE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Changed in version 2.7:&nbsp;<\/em>Added the optional flags argument.<code>re.<\/code><code>subn<\/code>(<em>pattern<\/em>,&nbsp;<em>repl<\/em>,&nbsp;<em>string<\/em>,&nbsp;<em>count=0<\/em>,&nbsp;<em>flags=0<\/em>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Perform the same operation as&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.sub\"><code>sub()<\/code><\/a>, but return a tuple&nbsp;<code>(new_string,&nbsp;number_of_subs_made)<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Changed in version 2.7:&nbsp;<\/em>Added the optional flags argument.<code>re.<\/code><code>escape<\/code>(<em>pattern<\/em>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Escape all the characters in&nbsp;<em>pattern<\/em>&nbsp;except ASCII letters and numbers. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>print re.escape('python.exe')\npython\\.exe\n\n<strong>&gt;&gt;&gt; <\/strong>legal_chars = string.ascii_lowercase + string.digits + \"!#$%&amp;'*+-.^_`|~:\"\n<strong>&gt;&gt;&gt; <\/strong>print '[<em>%s<\/em>]+' % re.escape(legal_chars)\n[abcdefghijklmnopqrstuvwxyz0123456789\\!\\#\\$\\%\\&amp;\\'\\*\\+\\-\\.\\^\\_\\`\\|\\~\\:]+\n\n<strong>&gt;&gt;&gt; <\/strong>operators = ['+', '-', '*', '\/', '**']\n<strong>&gt;&gt;&gt; <\/strong>print '|'.join(map(re.escape, sorted(operators, reverse=<strong>True<\/strong>)))\n\\\/|\\-|\\+|\\*\\*|\\*\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>re.<\/code><code>purge<\/code>()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clear the regular expression cache.<em>exception&nbsp;<\/em><code>re.<\/code><code>error<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\number<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches the contents of the group of the same number. Groups are numbered starting from 1. For example,\u00a0<code>(.+)\u00a0\\1<\/code>\u00a0matches\u00a0<code>'the\u00a0the'<\/code>\u00a0or\u00a0<code>'55\u00a055'<\/code>, but not\u00a0<code>'thethe'<\/code>\u00a0(note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of\u00a0<em>number<\/em>\u00a0is 0, or\u00a0<em>number<\/em>\u00a0is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value\u00a0<em>number<\/em>. Inside the\u00a0<code>'['<\/code>\u00a0and\u00a0<code>']'<\/code>\u00a0of a character class, all numeric escapes are treated as characters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\A<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches only at the start of the string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\b<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that formally,\u00a0<code>\\b<\/code>\u00a0is defined as the boundary between a\u00a0<code>\\w<\/code>\u00a0and a\u00a0<code>\\W<\/code>\u00a0character (or vice versa), or between\u00a0<code>\\w<\/code>\u00a0and the beginning\/end of the string, so the precise set of characters deemed to be alphanumeric depends on the values of the\u00a0<code>UNICODE<\/code>\u00a0and\u00a0<code>LOCALE<\/code>\u00a0flags. For example,\u00a0<code>r'\\bfoo\\b'<\/code>\u00a0matches\u00a0<code>'foo'<\/code>,\u00a0<code>'foo.'<\/code>,\u00a0<code>'(foo)'<\/code>,\u00a0<code>'bar\u00a0foo\u00a0baz'<\/code>\u00a0but not\u00a0<code>'foobar'<\/code>\u00a0or\u00a0<code>'foo3'<\/code>. Inside a character range,\u00a0<code>\\b<\/code>\u00a0represents the backspace character, for compatibility with Python\u2019s string literals.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\B<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches the empty string, but only when it is\u00a0<em>not<\/em>\u00a0at the beginning or end of a word. This means that\u00a0<code>r'py\\B'<\/code>\u00a0matches\u00a0<code>'python'<\/code>,\u00a0<code>'py3'<\/code>,\u00a0<code>'py2'<\/code>, but not\u00a0<code>'py'<\/code>,\u00a0<code>'py.'<\/code>, or\u00a0<code>'py!'<\/code>.\u00a0<code>\\B<\/code>\u00a0is just the opposite of\u00a0<code>\\b<\/code>, so is also subject to the settings of\u00a0<code>LOCALE<\/code>\u00a0and\u00a0<code>UNICODE<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\d<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flag is not specified, matches any decimal digit; this is equivalent to the set\u00a0<code>[0-9]<\/code>. With\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>, it will match whatever is classified as a decimal digit in the Unicode character properties database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\D<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flag is not specified, matches any non-digit character; this is equivalent to the set\u00a0<code>[^0-9]<\/code>. With\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>, it will match anything other than character marked as digits in the Unicode character properties database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\s<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flag is not specified, it matches any whitespace character, this is equivalent to the set\u00a0<code>[\u00a0\\t\\n\\r\\f\\v]<\/code>. The\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>\u00a0flag has no extra effect on matching of the space. If\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0is set, this will match the characters\u00a0<code>[\u00a0\\t\\n\\r\\f\\v]<\/code>\u00a0plus whatever is classified as space in the Unicode character properties database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\S<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flag is not specified, matches any non-whitespace character; this is equivalent to the set\u00a0<code>[^\u00a0\\t\\n\\r\\f\\v]<\/code>\u00a0The\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>\u00a0flag has no extra effect on non-whitespace match. If\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0is set, then any character not marked as space in the Unicode character properties database is matched.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\w<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>\u00a0and\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set\u00a0<code>[a-zA-Z0-9_]<\/code>. With\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>, it will match the set\u00a0<code>[0-9_]<\/code>\u00a0plus whatever characters are defined as alphanumeric for the current locale. If\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0is set, this will match the characters\u00a0<code>[0-9_]<\/code>\u00a0plus whatever is classified as alphanumeric in the Unicode character properties database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\W<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>\u00a0and\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0flags are not specified, matches any non-alphanumeric character; this is equivalent to the set\u00a0<code>[^a-zA-Z0-9_]<\/code>. With\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>, it will match any character not in the set\u00a0<code>[0-9_]<\/code>, and not defined as alphanumeric for the current locale. If\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>\u00a0is set, this will match anything other than\u00a0<code>[0-9_]<\/code>\u00a0plus characters classified as not alphanumeric in the Unicode character properties database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>\\Z<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches only at the end of the string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'.'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">(Dot.) In the default mode, this matches any character except a newline. If the\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.DOTALL\"><code>DOTALL<\/code><\/a>\u00a0flag has been specified, this matches any character including a newline.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'^'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">(Caret.) Matches the start of the string, and in\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MULTILINE\"><code>MULTILINE<\/code><\/a>\u00a0mode also matches immediately after each newline.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'$'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches the end of the string or just before the newline at the end of the string, and in\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MULTILINE\"><code>MULTILINE<\/code><\/a>\u00a0mode also matches before a newline.\u00a0<code>foo<\/code>\u00a0matches both \u2018foo\u2019 and \u2018foobar\u2019, while the regular expression\u00a0<code>foo$<\/code>\u00a0matches only \u2018foo\u2019. More interestingly, searching for\u00a0<code>foo.$<\/code>\u00a0in\u00a0<code>'foo1\\nfoo2\\n'<\/code>\u00a0matches \u2018foo2\u2019 normally, but \u2018foo1\u2019 in\u00a0<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.MULTILINE\"><code>MULTILINE<\/code><\/a>\u00a0mode; searching for a single\u00a0<code>$<\/code>\u00a0in\u00a0<code>'foo\\n'<\/code>\u00a0will find two (empty) matches: one just before the newline, and one at the end of the string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'*'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">It causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible.\u00a0<code>ab*<\/code>\u00a0will match \u2018a\u2019, \u2018ab\u2019, or \u2018a\u2019 followed by any number of \u2018b\u2019s.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'+'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Causes the resulting RE to match 1 or more repetitions of the preceding RE.\u00a0<code>ab+<\/code>\u00a0will match \u2018a\u2019 followed by any non-zero number of \u2018b\u2019s; it will not match just \u2018a\u2019.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'?'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Causes the resulting RE to match 0 or 1 repetitions of the preceding RE.\u00a0<code>ab?<\/code>\u00a0will match either \u2018a\u2019 or \u2018ab\u2019.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>*?<\/code>,\u00a0<code>+?<\/code>,\u00a0<code>??<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The\u00a0<code>'*'<\/code>,\u00a0<code>'+'<\/code>, and\u00a0<code>'?'<\/code>\u00a0qualifiers are all\u00a0<em>greedy<\/em>; they match as much text as possible. Sometimes this behaviour isn\u2019t desired; if the RE\u00a0<code>&lt;.*><\/code>\u00a0is matched against\u00a0<code>&lt;a>\u00a0b\u00a0&lt;c><\/code>, it will match the entire string, and not just\u00a0<code>&lt;a><\/code>. Adding\u00a0<code>?<\/code>\u00a0after the qualifier makes it perform the match in\u00a0<em>non-greedy<\/em>\u00a0or\u00a0<em>minimal<\/em>\u00a0fashion; as\u00a0<em>few<\/em>\u00a0characters as possible will be matched. Using the RE\u00a0<code>&lt;.*?><\/code>\u00a0will match only\u00a0<code>&lt;a><\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>{m}<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Specifies that exactly\u00a0<em>m<\/em>\u00a0copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example,\u00a0<code>a{6}<\/code>\u00a0will match exactly six\u00a0<code>'a'<\/code>\u00a0characters, but not five.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>{m,n}<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Causes the resulting RE to match from\u00a0<em>m<\/em>\u00a0to\u00a0<em>n<\/em>\u00a0repetitions of the preceding RE, attempting to match as many repetitions as possible. For example,\u00a0<code>a{3,5}<\/code>\u00a0will match from 3 to 5\u00a0<code>'a'<\/code>\u00a0characters. Omitting\u00a0<em>m<\/em>\u00a0specifies a lower bound of zero, and omitting\u00a0<em>n<\/em>\u00a0specifies an infinite upper bound. As an example,\u00a0<code>a{4,}b<\/code>\u00a0will match\u00a0<code>aaaab<\/code>\u00a0or a thousand\u00a0<code>'a'<\/code>\u00a0characters followed by a\u00a0<code>b<\/code>, but not\u00a0<code>aaab<\/code>. The comma may not be omitted or the modifier would be confused with the previously described form.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>{m,n}?<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Causes the resulting RE to match from\u00a0<em>m<\/em>\u00a0to\u00a0<em>n<\/em>\u00a0repetitions of the preceding RE, attempting to match as\u00a0<em>few<\/em>\u00a0repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string\u00a0<code>'aaaaaa'<\/code>,\u00a0<code>a{3,5}<\/code>\u00a0will match 5\u00a0<code>'a'<\/code>\u00a0characters, while\u00a0<code>a{3,5}?<\/code>\u00a0will only match 3 characters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'\\'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Either escapes special characters (permitting you to match characters like&nbsp;<code>'*'<\/code>,&nbsp;<code>'?'<\/code>, and so forth), or signals a special sequence; special sequences are discussed below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn\u2019t recognized by Python\u2019s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it\u2019s highly recommended that you use raw strings for all but the simplest expressions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>[]<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to indicate a set of characters. In a set:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Characters can be listed individually, e.g.&nbsp;<code>[amk]<\/code>&nbsp;will match&nbsp;<code>'a'<\/code>,&nbsp;<code>'m'<\/code>, or&nbsp;<code>'k'<\/code>.<\/li><li>Ranges of characters can be indicated by giving two characters and separating them by a&nbsp;<code>'-'<\/code>, for example&nbsp;<code>[a-z]<\/code>&nbsp;will match any lowercase ASCII letter,&nbsp;<code>[0-5][0-9]<\/code>&nbsp;will match all the two-digits numbers from&nbsp;<code>00<\/code>&nbsp;to&nbsp;<code>59<\/code>, and&nbsp;<code>[0-9A-Fa-f]<\/code>&nbsp;will match any hexadecimal digit. If&nbsp;<code>-<\/code>&nbsp;is escaped (e.g.&nbsp;<code>[a\\-z]<\/code>) or if it\u2019s placed as the first or last character (e.g.&nbsp;<code>[a-]<\/code>), it will match a literal&nbsp;<code>'-'<\/code>.<\/li><li>Special characters lose their special meaning inside sets. For example,&nbsp;<code>[(+*)]<\/code>&nbsp;will match any of the literal characters&nbsp;<code>'('<\/code>,&nbsp;<code>'+'<\/code>,&nbsp;<code>'*'<\/code>, or&nbsp;<code>')'<\/code>.<\/li><li>Character classes such as&nbsp;<code>\\w<\/code>&nbsp;or&nbsp;<code>\\S<\/code>&nbsp;(defined below) are also accepted inside a set, although the characters they match depends on whether&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.LOCALE\"><code>LOCALE<\/code><\/a>&nbsp;or&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.UNICODE\"><code>UNICODE<\/code><\/a>&nbsp;mode is in force.<\/li><li>Characters that are not within a range can be matched by&nbsp;<em>complementing<\/em>&nbsp;the set. If the first character of the set is&nbsp;<code>'^'<\/code>, all the characters that are&nbsp;<em>not<\/em>&nbsp;in the set will be matched. For example,&nbsp;<code>[^5]<\/code>&nbsp;will match any character except&nbsp;<code>'5'<\/code>, and&nbsp;<code>[^^]<\/code>&nbsp;will match any character except&nbsp;<code>'^'<\/code>.&nbsp;<code>^<\/code>&nbsp;has no special meaning if it\u2019s not the first character in the set.<\/li><li>To match a literal&nbsp;<code>']'<\/code>&nbsp;inside a set, precede it with a backslash, or place it at the beginning of the set. For example, both&nbsp;<code>[()[\\]{}]<\/code>&nbsp;and&nbsp;<code>[]()[{}]<\/code>&nbsp;will both match a parenthesis.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><code>'|'<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>A|B<\/code>, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the\u00a0<code>'|'<\/code>\u00a0in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by\u00a0<code>'|'<\/code>\u00a0are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once\u00a0<code>A<\/code>\u00a0matches,\u00a0<code>B<\/code>\u00a0will not be tested further, even if it would produce a longer overall match. In other words, the\u00a0<code>'|'<\/code>\u00a0operator is never greedy. To match a literal\u00a0<code>'|'<\/code>, use\u00a0<code>\\|<\/code>, or enclose it inside a character class, as in\u00a0<code>[|]<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(...)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the\u00a0<code>\\number<\/code>\u00a0special sequence, described below. To match the literals\u00a0<code>'('<\/code>\u00a0or\u00a0<code>')'<\/code>, use\u00a0<code>\\(<\/code>\u00a0or\u00a0<code>\\)<\/code>, or enclose them inside a character class:\u00a0<code>[(]\u00a0[)]<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(?...)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This is an extension notation (a\u00a0<code>'?'<\/code>\u00a0following a\u00a0<code>'('<\/code>\u00a0is not meaningful otherwise). The first character after the\u00a0<code>'?'<\/code>\u00a0determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group;\u00a0<code>(?P&lt;name>...)<\/code>\u00a0is the only exception to this rule. Following are the currently supported extensions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(?iLmsux)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">(One or more letters from the set&nbsp;<code>'i'<\/code>,&nbsp;<code>'L'<\/code>,&nbsp;<code>'m'<\/code>,&nbsp;<code>'s'<\/code>,&nbsp;<code>'u'<\/code>,&nbsp;<code>'x'<\/code>.) The group matches the empty string; the letters set the corresponding flags:&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.I\"><code>re.I<\/code><\/a>&nbsp;(ignore case),&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.L\"><code>re.L<\/code><\/a>&nbsp;(locale dependent),&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.M\"><code>re.M<\/code><\/a>&nbsp;(multi-line),&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.S\"><code>re.S<\/code><\/a>&nbsp;(dot matches all),&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.U\"><code>re.U<\/code><\/a>&nbsp;(Unicode dependent), and&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.X\"><code>re.X<\/code><\/a>&nbsp;(verbose), for the entire regular expression. (The flags are described in&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#contents-of-module-re\">Module Contents<\/a>.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a&nbsp;<em>flag<\/em>&nbsp;argument to the&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.compile\"><code>re.compile()<\/code><\/a>&nbsp;function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the\u00a0<code>(?x)<\/code>\u00a0flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(?:...)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group\u00a0<em>cannot<\/em>\u00a0be retrieved after performing a match or referenced later in the pattern.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(?P&lt;name>...)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name&nbsp;<em>name<\/em>. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Named groups can be referenced in three contexts. If the pattern is&nbsp;<code>(?P&lt;quote&gt;['\"]).*?(?P=quote)<\/code>&nbsp;(i.e. matching a string quoted with either single or double quotes):<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>Context of reference to group \u201cquote\u201d<\/th><th>Ways to reference it<\/th><\/tr><\/thead><tbody><tr><td>in the same pattern itself<\/td><td><code>(?P=quote)<\/code>&nbsp;(as shown)<code>\\1<\/code><\/td><\/tr><tr><td>when processing match object&nbsp;<code>m<\/code><\/td><td><code>m.group('quote')<\/code><code>m.end('quote')<\/code>&nbsp;(etc.)<\/td><\/tr><tr><td>in a string passed to the&nbsp;<code>repl<\/code>&nbsp;argument of&nbsp;<code>re.sub()<\/code><\/td><td><code>\\g&lt;quote&gt;<\/code><code>\\g&lt;1&gt;<\/code><code>\\1<\/code><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p class=\"wp-block-paragraph\"><code>(?P=name)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A backreference to a named group; it matches whatever text was matched by the earlier group named&nbsp;<em>name<\/em>.<code>(?#...)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A comment; the contents of the parentheses are simply ignored.<code>(?=...)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Matches if&nbsp;<code>...<\/code>&nbsp;matches next, but doesn\u2019t consume any of the string. This is called a lookahead assertion. For example,&nbsp;<code>Isaac&nbsp;(?=Asimov)<\/code>&nbsp;will match&nbsp;<code>'Isaac&nbsp;'<\/code>&nbsp;only if it\u2019s followed by&nbsp;<code>'Asimov'<\/code>.<code>(?!...)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Matches if&nbsp;<code>...<\/code>&nbsp;doesn\u2019t match next. This is a negative lookahead assertion. For example,&nbsp;<code>Isaac&nbsp;(?!Asimov)<\/code>&nbsp;will match&nbsp;<code>'Isaac&nbsp;'<\/code>&nbsp;only if it\u2019s&nbsp;<em>not<\/em>&nbsp;followed by&nbsp;<code>'Asimov'<\/code>.<code>(?&lt;=...)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Matches if the current position in the string is preceded by a match for&nbsp;<code>...<\/code>&nbsp;that ends at the current position. This is called a&nbsp;<em>positive lookbehind assertion<\/em>.&nbsp;<code>(?&lt;=abc)def<\/code>&nbsp;will find a match in&nbsp;<code>abcdef<\/code>, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that&nbsp;<code>abc<\/code>&nbsp;or&nbsp;<code>a|b<\/code>&nbsp;are allowed, but&nbsp;<code>a*<\/code>&nbsp;and&nbsp;<code>a{3,4}<\/code>&nbsp;are not. Group references are not supported even if they match strings of some fixed length. Note that patterns which start with positive lookbehind assertions will not match at the beginning of the string being searched; you will most likely want to use the&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.search\"><code>search()<\/code><\/a>&nbsp;function rather than the&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/re.html#re.match\"><code>match()<\/code><\/a>&nbsp;function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong> <strong>re<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>m = re.search('(?&lt;=abc)def', 'abcdef')\n<strong>&gt;&gt;&gt; <\/strong>m.group(0)\n'def'\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example looks for a word following a hyphen:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>m = re.search('(?&lt;=-)\\w+', 'spam-egg')\n<strong>&gt;&gt;&gt; <\/strong>m.group(0)\n'egg'\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><code>(?&lt;!...)<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Matches if the current position in the string is not preceded by a match for&nbsp;<code>...<\/code>. This is called a&nbsp;<em>negative lookbehind assertion<\/em>. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length and shouldn\u2019t contain group references. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.<code>(?(id\/name)yes-pattern|no-pattern)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Will try to match with&nbsp;<code>yes-pattern<\/code>&nbsp;if the group with given&nbsp;<em>id<\/em>&nbsp;or&nbsp;<em>name<\/em>&nbsp;exists, and with&nbsp;<code>no-pattern<\/code>&nbsp;if it doesn\u2019t.&nbsp;<code>no-pattern<\/code>&nbsp;is optional and can be omitted. For example,&nbsp;<code>(&lt;)?(\\w+@\\w+(?:\\.\\w+)+)(?(1)&gt;)<\/code>&nbsp;is a poor email matching pattern, which will match with&nbsp;<code>'&lt;user@host.com&gt;'<\/code>&nbsp;as well as&nbsp;<code>'user@host.com'<\/code>, but not with&nbsp;<code>'&lt;user@host.com'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The module defines several functions, constants, and an exception. Some of the functions are simplified versions of the full-featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form. re.compile(pattern,\u00a0flags=0) Compile a regular expression pattern into a regular expression object, which can be used for matching using its&nbsp;match()&nbsp;and&nbsp;search()&nbsp;methods, described below. The expression\u2019s &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1846,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":{"facebook_2613112545524186_272996453239123":"","twitter_aTo5NzY3MTIyNTIwMzEwNTM4MjQ7_976712252031053800":""},"rop_publish_now_history":[],"rop_publish_now_status":"pending","footnotes":""},"categories":[24],"tags":[],"class_list":["post-951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Regular Expression In Python - Learn Scripting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnscripting.org\/regular-expression-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Regular Expression In Python - Learn Scripting\" \/>\n<meta property=\"og:description\" content=\"The module defines several functions, constants, and an exception. Some of the functions are simplified versions of the full-featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form. re.compile(pattern,\u00a0flags=0) Compile a regular expression pattern into a regular expression object, which can be used for matching using its&nbsp;match()&nbsp;and&nbsp;search()&nbsp;methods, described below. The expression\u2019s &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnscripting.org\/regular-expression-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Scripting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=100064270142636\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-23T09:09:35+00:00\" \/>\n<meta name=\"author\" content=\"Shakti Das\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shakti Das\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/\"},\"author\":{\"name\":\"Shakti Das\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\"},\"headline\":\"Regular Expression In Python\",\"datePublished\":\"2019-09-23T09:09:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/\"},\"wordCount\":3570,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/\",\"name\":\"Regular Expression In Python - Learn Scripting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"datePublished\":\"2019-09-23T09:09:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/regular-expression-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/learnscripting.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Regular Expression In Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"name\":\"Learn Scripting\",\"description\":\"Coding Knowledge Unveiled: Empower Yourself\",\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/learnscripting.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\",\"name\":\"Learn Scripting\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"Learn Scripting\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100064270142636\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\",\"name\":\"Shakti Das\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"caption\":\"Shakti Das\"},\"sameAs\":[\"https:\\\/\\\/learnscripting.org\"],\"url\":\"https:\\\/\\\/learnscripting.org\\\/author\\\/53kgl\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Regular Expression In Python - Learn Scripting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnscripting.org\/regular-expression-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Regular Expression In Python - Learn Scripting","og_description":"The module defines several functions, constants, and an exception. Some of the functions are simplified versions of the full-featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form. re.compile(pattern,\u00a0flags=0) Compile a regular expression pattern into a regular expression object, which can be used for matching using its&nbsp;match()&nbsp;and&nbsp;search()&nbsp;methods, described below. The expression\u2019s &hellip;","og_url":"https:\/\/learnscripting.org\/regular-expression-in-python\/","og_site_name":"Learn Scripting","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=100064270142636","article_published_time":"2019-09-23T09:09:35+00:00","author":"Shakti Das","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shakti Das","Est. reading time":"22 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#article","isPartOf":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/"},"author":{"name":"Shakti Das","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c"},"headline":"Regular Expression In Python","datePublished":"2019-09-23T09:09:35+00:00","mainEntityOfPage":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/"},"wordCount":3570,"commentCount":0,"publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"image":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#primaryimage"},"thumbnailUrl":"","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/learnscripting.org\/regular-expression-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/","url":"https:\/\/learnscripting.org\/regular-expression-in-python\/","name":"Regular Expression In Python - Learn Scripting","isPartOf":{"@id":"https:\/\/learnscripting.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#primaryimage"},"image":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#primaryimage"},"thumbnailUrl":"","datePublished":"2019-09-23T09:09:35+00:00","breadcrumb":{"@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnscripting.org\/regular-expression-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/learnscripting.org\/regular-expression-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnscripting.org\/"},{"@type":"ListItem","position":2,"name":"Regular Expression In Python"}]},{"@type":"WebSite","@id":"https:\/\/learnscripting.org\/#website","url":"https:\/\/learnscripting.org\/","name":"Learn Scripting","description":"Coding Knowledge Unveiled: Empower Yourself","publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnscripting.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/learnscripting.org\/#organization","name":"Learn Scripting","url":"https:\/\/learnscripting.org\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Learn Scripting"},"image":{"@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/profile.php?id=100064270142636"]},{"@type":"Person","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c","name":"Shakti Das","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","caption":"Shakti Das"},"sameAs":["https:\/\/learnscripting.org"],"url":"https:\/\/learnscripting.org\/author\/53kgl\/"}]}},"_links":{"self":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":0,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"wp:attachment":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}