{"id":403,"date":"2024-01-29T10:41:38","date_gmt":"2024-01-29T10:41:38","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=403"},"modified":"2024-01-29T10:41:44","modified_gmt":"2024-01-29T10:41:44","slug":"python-regex-cheat-sheet","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/29\/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\u00a0regular expressions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Character sets<\/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\">Anchors<\/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\">Quantifiers<\/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&nbsp;<code>n<\/code>&nbsp;times.<\/td><\/tr><tr><td><code>{n , }<\/code><\/td><td><code>{n,}?<\/code><\/td><td>Match its preceding element at least&nbsp;<code>n<\/code>&nbsp;times.<\/td><\/tr><tr><td><code>{n , m}<\/code><\/td><td><code>{n , m}?<\/code><\/td><td>Match its preceding element from&nbsp;<code>n<\/code>&nbsp;to&nbsp;<code>m<\/code>&nbsp;times<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Sets &amp; Ranges<\/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&nbsp;<code>n<\/code>&nbsp;times.<\/td><\/tr><tr><td><code>{n , m}<\/code><\/td><td>Match its preceding element from&nbsp;<code>n<\/code>&nbsp;to&nbsp;<code>m<\/code>&nbsp;times<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Capturing Groups<\/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&gt;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&gt;<\/code><\/td><td>Reference the capturing group #N (alternative syntax)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Alternation<\/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\">Look Around<\/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>(?&lt;=Y)X<\/code><\/td><td>Match X if there is Y before it<\/td><\/tr><tr><td><code>(?&lt;!Y)X<\/code><\/td><td>Match X if there is NO Y before it<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Regex functions<\/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><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-search\/\">search()<\/a><\/code><\/td><td>Return the first match<\/td><\/tr><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-fullmatch\/\">fullmatch()<\/a><\/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\">Regex Flags<\/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","protected":false},"excerpt":{"rendered":"<p>This page provides a Python regex cheat sheet that you can quickly reference while working with\u00a0regular expressions. Character sets Pattern Meaning \\w Match a single word character a-z, A-Z, 0-9, and underscore (_) \\d Match a single digit 0-9 \\s Match whitespace including \\t, \\n, and \\r and space character . Match any character except [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-2-python-regex"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=403"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":404,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/403\/revisions\/404"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}