{"id":3377,"date":"2021-12-10T01:19:00","date_gmt":"2021-12-10T01:19:00","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3377"},"modified":"2021-12-10T01:28:15","modified_gmt":"2021-12-10T01:28:15","slug":"python-regex-cheat-sheet","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-cheat-sheet\/","title":{"rendered":"Python Regex Cheat Sheet"},"content":{"rendered":"\n<p>This page provides a Python regex cheat sheet that you can quickly reference while working with <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\">regular expressions<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='character-sets'>Character sets <a href=\"#character-sets\" class=\"anchor\" id=\"character-sets\" title=\"Anchor for Character sets\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>\\w<\/code><\/td><td>Match a single word character a-z, A-Z, 0-9, and underscore (_)<\/td><\/tr><tr><td><code>\\d<\/code><\/td><td>Match a single digit 0-9<\/td><\/tr><tr><td><code>\\s<\/code><\/td><td>Match whitespace including  \\t, \\n, and \\r and space character<\/td><\/tr><tr><td><code>.<\/code><\/td><td>Match any character except the newline<\/td><\/tr><tr><td><code>\\W<\/code><\/td><td>Match a character except for a word character<\/td><\/tr><tr><td><code>\\D<\/code><\/td><td>Match a character except for a digit<\/td><\/tr><tr><td><code>\\S<\/code><\/td><td>Match a single character except for a whitespace character<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='anchors'>Anchors <a href=\"#anchors\" class=\"anchor\" id=\"anchors\" title=\"Anchor for Anchors\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>^<\/code><\/td><td>Match at the beginning of a string<\/td><\/tr><tr><td><code>$<\/code><\/td><td>Match at the end of a string<\/td><\/tr><tr><td><code>\\b<\/code><\/td><td>Match a position defined as a word boundary<\/td><\/tr><tr><td><code>\\B<\/code><\/td><td>Match a position that is not a word boundary<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='quantifiers'>Quantifiers <a href=\"#quantifiers\" class=\"anchor\" id=\"quantifiers\" title=\"Anchor for Quantifiers\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Quantifiers (Greedy)<\/th><th>Non-greedy Quantifiers (Lazy)<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>*<\/code><\/td><td><code>*?<\/code><\/td><td>Match its preceding element zero or more times.<\/td><\/tr><tr><td><code>+<\/code><\/td><td><code>+?<\/code><\/td><td>Match its preceding element one or more times.<\/td><\/tr><tr><td><code>?<\/code><\/td><td><code>??<\/code><\/td><td>Match its preceding element zero or one time.<\/td><\/tr><tr><td><code>{n}<\/code><\/td><td><code>{n}?<\/code><\/td><td>Match its preceding element exactly\u00a0<code>n<\/code>\u00a0times.<\/td><\/tr><tr><td><code>{n , }<\/code><\/td><td><code>{n,}?<\/code><\/td><td>Match its preceding element at least\u00a0<code>n<\/code>\u00a0times.<\/td><\/tr><tr><td><code>{n , m}<\/code><\/td><td><code>{n , m}?<\/code><\/td><td>Match its preceding element from\u00a0<code>n<\/code>\u00a0to\u00a0<code>m<\/code>\u00a0times<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='sets-ranges'>Sets &amp; Ranges <a href=\"#sets-ranges\" class=\"anchor\" id=\"sets-ranges\" title=\"Anchor for Sets &amp; Ranges\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>[XYZ]<\/code><\/td><td>Match any of three elements X, Y, and Z<\/td><\/tr><tr><td><code>[X-Y]<\/code><\/td><td>Match a range from X to Y <\/td><\/tr><tr><td><code>^[XYZ]<\/code><\/td><td>Match any single element except X, Y, and Z<\/td><\/tr><tr><td><code>^[X-Y]<\/code><\/td><td>Match any single element <\/td><\/tr><tr><td><code>{n , }<\/code><\/td><td>Match its preceding element at least\u00a0<code>n<\/code>\u00a0times.<\/td><\/tr><tr><td><code>{n , m}<\/code><\/td><td>Match its preceding element from\u00a0<code>n<\/code>\u00a0to\u00a0<code>m<\/code>\u00a0times<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='capturing-groups'>Capturing Groups <a href=\"#capturing-groups\" class=\"anchor\" id=\"capturing-groups\" title=\"Anchor for Capturing Groups\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>(X)<\/code><\/td><td>Capture the X in the group<\/td><\/tr><tr><td><code>(?P&lt;name>X)<\/code><\/td><td>Capture the X and assign it the name<\/td><\/tr><tr><td><code>\\N<\/code><\/td><td>Reference the capturing group #N<\/td><\/tr><tr><td><code>\\g&lt;N><\/code><\/td><td>Reference the capturing group #N (alternative syntax)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='alternation'>Alternation <a href=\"#alternation\" class=\"anchor\" id=\"alternation\" title=\"Anchor for Alternation\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>X | Y<\/code><\/td><td>Match either X or Y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='look-around'>Look Around <a href=\"#look-around\" class=\"anchor\" id=\"look-around\" title=\"Anchor for Look Around\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>X(?=Y)<\/code><\/td><td>Match X but only if it is followed by Y<\/td><\/tr><tr><td><code>X(?!Y)<\/code><\/td><td>Match X but only if it is NOT followed by Y<\/td><\/tr><tr><td><code><code>(?&lt;=Y)X<\/code><\/code><\/td><td>Match X if there is Y before it<\/td><\/tr><tr><td><code><code>(?&lt;!Y)X<\/code><\/code><\/td><td>Match X if there is NO Y before it<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='regex-functions'>Regex functions <a href=\"#regex-functions\" class=\"anchor\" id=\"regex-functions\" title=\"Anchor for Regex functions\">#<\/a><\/h2>\n\n\n\n<p>The following table shows the regex function from the re module.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Function<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-findall\/\">findall()<\/a><\/code><\/td><td>Return a list of matches or None<\/td><\/tr><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-finditer\/\">finditer()<\/a><\/code><\/td><td>Return an iterator yielding all non-overlapping matches<\/td><\/tr><tr><td><code><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-search\/\">search()<\/a><\/code><\/code><\/td><td>Return the first match<\/td><\/tr><tr><td><code><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-fullmatch\/\">fullmatch()<\/a><\/code><\/code><\/td><td>Return a Match object if the whole string matches a pattern<\/td><\/tr><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-match\/\">match()<\/a><\/code><\/td><td>Return the match at the beginning of a string or None<\/td><\/tr><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-sub\/\">sub()<\/a><\/code><\/td><td>Return a string with matched replaced with a replacement<\/td><\/tr><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-split\/\">split()<\/a><\/code><\/td><td>Split a string at the occurrences of matches <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='regex-flags'>Regex Flags <a href=\"#regex-flags\" class=\"anchor\" id=\"regex-flags\" title=\"Anchor for Regex Flags\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Flag<\/th><th>Alias<\/th><th>Inline Flag<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>re.ASCII<\/code><\/td><td><code>re.A<\/code><\/td><td><code>?m<\/code><\/td><td>The&nbsp;<code>re.ASCII<\/code>&nbsp;is relevant to the byte patterns only. It makes the&nbsp;<code>\\w<\/code>,&nbsp;<code>\\W<\/code>,<code>\\b<\/code>,&nbsp;<code>\\B<\/code>,&nbsp;<code>\\d<\/code>, \\D, and&nbsp;<code>\\S<\/code>&nbsp;perform ASCII-only matching instead of full Unicode matching.<\/td><\/tr><tr><td><code>re.DEBUG<\/code><\/td><td>N\/A<\/td><td>N\/A<\/td><td>The&nbsp;<code>re.DEBUG<\/code>&nbsp;shows the debug information of compiled pattern.<\/td><\/tr><tr><td><code>re.IGNORECASE<\/code><\/td><td><code>re.I<\/code><\/td><td><code>?i<\/code><\/td><td>perform case-insensitive matching. It means that the&nbsp;<code>[A-Z]<\/code>&nbsp;will also match lowercase letters.<\/td><\/tr><tr><td><code>re.LOCALE<\/code><\/td><td><code>re.L<\/code><\/td><td><code>?L<\/code><\/td><td>The&nbsp;<code>re.LOCALE<\/code>&nbsp;is relevant only to the byte pattern. It makes the&nbsp;<code>\\w<\/code>,&nbsp;<code>\\W<\/code>,&nbsp;<code>\\b<\/code>,&nbsp;<code>\\B<\/code>&nbsp;and case-sensitive matching dependent on the current locale. The&nbsp;<code>re.LOCALE<\/code>&nbsp;is not compatible with the&nbsp;<code>re.ASCII<\/code>&nbsp;flag.<\/td><\/tr><tr><td><code>re.MUTILINE<\/code><\/td><td><code>re.M<\/code><\/td><td><code>?m<\/code><\/td><td>The&nbsp;<code>re.MULTILINE<\/code>&nbsp;makes the&nbsp;<code>^<\/code>&nbsp;matches at the beginning of a string and at the beginning of each line and&nbsp;<code>$<\/code>&nbsp;matches at the end of a string and at the end of each line.<\/td><\/tr><tr><td><code>re.DOTALL<\/code><\/td><td><code>re.S<\/code><\/td><td><code>?s<\/code><\/td><td>By default, the dot (<code>.<\/code>) matches any characters except a newline. The&nbsp;<code>re.DOTALL<\/code>&nbsp;makes the dot (<code>.<\/code>) matches all characters including a newline.<\/td><\/tr><tr><td><code>re.VERBOSE<\/code><\/td><td><code>re.X<\/code><\/td><td><code>?x<\/code><\/td><td>The&nbsp;<code>re.VERBOSE<\/code>&nbsp;flag allows you to organize a pattern into logical sections visually and add comments.<\/td><\/tr><\/tbody><\/table><\/figure>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3377\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-cheat-sheet\/\"\n\t\t\t\tdata-post-title=\"Python Regex Cheat Sheet\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3377\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-cheat-sheet\/\"\n\t\t\t\tdata-post-title=\"Python Regex Cheat Sheet\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Provide a Python regex cheat sheet that  you can quickly reference while working with regular expressions<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3122,"menu_order":22,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3377","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3377"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3377\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3122"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}