{"id":3122,"date":"2021-11-29T01:19:50","date_gmt":"2021-11-29T01:19:50","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3122"},"modified":"2023-05-31T10:25:23","modified_gmt":"2023-05-31T10:25:23","slug":"python-regex","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-regex\/","title":{"rendered":"Python Regex"},"content":{"rendered":"\n<p>A regular expression (or regex) is a sequence of characters that specifies a search pattern. In practice, you&#8217;ll find the regular expressions in many applications such as search engines, search and replace dialogs of text editors.<\/p>\n\n\n\n<p>In Python, a regular expression is a separate programming language. It is embedded in Python. To interact with the regular expressions, you use the built-in <code>re<\/code> module:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-regular-expressions.svg\" alt=\"Python Regex\" class=\"wp-image-3131\"\/><\/figure>\n<\/div>\n\n\n<p>The <code>re<\/code> module has many helpful classes and functions that help you to manage regular expressions effectively.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='basic-regular-expressions'>Section 1. Basic Regular Expressions <a href=\"#basic-regular-expressions\" class=\"anchor\" id=\"basic-regular-expressions\" title=\"Anchor for Section 1. Basic Regular Expressions\">#<\/a><\/h2>\n\n\n\n<p>This section introduces the regular expression basics including functions, character sets, anchors, and word boundaries.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\">Regular expressions<\/a>\u00a0\u2013 learn how regular expressions work in Python and how to use functions in the <code>re<\/code> module to match a string for a pattern.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-character-set\/\">Character sets<\/a> \u2013 introduce to you the character sets (<code>\\d<\/code>,\u00a0<code>\\w<\/code>,\u00a0<code>\\s<\/code>) that match digits, word characters, and spaces.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-anchors\/\">Anchors<\/a>&nbsp;\u2013 show you how to use the caret (<code>^<\/code>) and dollar (<code>$<\/code>) anchors to match at the beginning and end of a string.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-word-boundary\/\">Word Boundary<\/a>&nbsp;\u2013 guide you on how to use word boundary (<code>\\b<\/code>) to match the whole word.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='quantifiers'>Section 2. Quantifiers <a href=\"#quantifiers\" class=\"anchor\" id=\"quantifiers\" title=\"Anchor for Section 2. Quantifiers\">#<\/a><\/h2>\n\n\n\n<p>This section shows you how to apply quantifiers to specific parts and two modes of quantifiers including greedy and lazy.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-quantifiers\/\">Quantifiers<\/a>&nbsp;\u2013 learn how to use the quantifiers to match a number of instances of a character or character class.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-greedy\/\">Greedy Quantifiers<\/a>&nbsp;\u2013 learn how the greedy quantifiers work.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-non-greedy\/\">Non-greedy (or lazy) Quantifiers<\/a>\u00a0\u2013 explain how non-greedy quantifiers works and show you how to turn greedy quantifiers into non-greedy quantifiers.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='sets-and-ranges'>Section 3. Sets and Ranges <a href=\"#sets-and-ranges\" class=\"anchor\" id=\"sets-and-ranges\" title=\"Anchor for Section 3. Sets and Ranges\">#<\/a><\/h2>\n\n\n\n<p>This section covers the sets and ranges to match the characters.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-sets-ranges\/\">Sets &amp; Ranges<\/a>&nbsp;\u2013 learn how to use the sets and ranges to match a character in a set or range of characters.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='grouping'>Section 4. Grouping <a href=\"#grouping\" class=\"anchor\" id=\"grouping\" title=\"Anchor for Section 4. Grouping\">#<\/a><\/h2>\n\n\n\n<p>This section shows you how to use the grouping to extract portions of information, reference the preceding group, and apply alternation.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-capturing-group\/\">Capturing groups<\/a>&nbsp;\u2013 include a part of a match in the matches array and assign it a name using a named group.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-backreferences\/\">Backreferences<\/a>&nbsp;\u2013 learn about backreferences and how to apply them effectively.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-alternation\/\">Alternation<\/a>&nbsp;\u2013 show you how to use the \u201cOR\u201d operator in regular expressions.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-non-capturing-group\/\">Non-capturing groups<\/a> &#8211; create a group but don&#8217;t save it in the groups of the match.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='look-around'>Section 5. Look Around <a href=\"#look-around\" class=\"anchor\" id=\"look-around\" title=\"Anchor for Section 5. Look Around\">#<\/a><\/h2>\n\n\n\n<p>This section explains the concepts of different types of look-around mechanisms.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-lookahead\/\">Lookahead<\/a>&nbsp;\u2013 introduce to you the lookahead concept and how to use it to match X only if it is followed by Y.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-lookbehind\/\">Lookbehind<\/a>\u00a0\u2013 explain the lookbehind concept and how to match X only if there is Y before it.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='python-regex-functions'>Section 6. Python regex functions <a href=\"#python-regex-functions\" class=\"anchor\" id=\"python-regex-functions\" title=\"Anchor for Section 6. Python regex functions\">#<\/a><\/h2>\n\n\n\n<p>This section discusses the regular expression functions in the <code>re<\/code> module in detail and the regex flags.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-findall\/\">findall()<\/a>&nbsp;\u2013 find all matches that match a regular expression in a string.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-fullmatch\/\">fullmatch()<\/a> &#8211; match the whole string with a pattern.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-finditer\/\">finditer()<\/a> &#8211; return an iterator yielding Match objects over all non-overlapping matches for a regular expression in a string.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-match\/\">match()<\/a> &#8211; check if zero or more characters at the beginning of a string match a regular expression.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-search\/\">search()<\/a>&nbsp;\u2013 search for the first match in a string.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-sub\/\">sub()<\/a> &#8211; return a string with matches replaced with a replacement.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-split\/\">split()<\/a> &#8211; split a string by the matches of a regular expression.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-flags\/\">Regex flags<\/a> &#8211; learn about the regex flags and how they change the way the regex engine matches patterns.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='python-regex-cheat-sheet'>Section 7. Python Regex Cheat Sheet <a href=\"#python-regex-cheat-sheet\" class=\"anchor\" id=\"python-regex-cheat-sheet\" title=\"Anchor for Section 7. Python Regex Cheat Sheet\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-cheat-sheet\/\">Regex Cheat Sheet<\/a>\u00a0\u2013 provide you with a Python regular expression cheat sheet.<\/li>\n<\/ul>\n<\/div><\/div>\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=\"3122\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/\"\n\t\t\t\tdata-post-title=\"Python Regex\"\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=\"3122\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/\"\n\t\t\t\tdata-post-title=\"Python Regex\"\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>In this section, you&#8217;ll learn about regular expressions in Python step by step from scratch.<\/p>\n","protected":false},"author":1,"featured_media":3263,"parent":0,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3122","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3122","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=3122"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media\/3263"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}