{"id":3278,"date":"2021-12-06T05:00:23","date_gmt":"2021-12-06T05:00:23","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3278"},"modified":"2021-12-10T01:57:36","modified_gmt":"2021-12-10T01:57:36","slug":"python-regex-findall","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-findall\/","title":{"rendered":"Python Regex findall()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python regex <code>findall()<\/code> function to find all matches of a pattern in a string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-regex-findall-function'>Introduction to the Python regex findall() function <a href=\"#introduction-to-the-python-regex-findall-function\" class=\"anchor\" id=\"introduction-to-the-python-regex-findall-function\" title=\"Anchor for Introduction to the Python regex findall() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>findall()<\/code> is a built-in <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> in the <code>re<\/code> module that handles <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\">regular expressions<\/a>. The <code>findall()<\/code> function has the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">re.findall(pattern, string, flags=<span class=\"hljs-number\">0<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>pattern<\/code> is a regular expression that you want to match.<\/li><li><code>string<\/code> is the input string<\/li><li><code>flags<\/code> is one or more <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-flags\/\">regular expression flags<\/a> that modify the standard behavior of the pattern.<\/li><\/ul>\n\n\n\n<p>The <code>findall()<\/code> function scans the <code>string<\/code> from left to right and finds all the matches of the <code>pattern<\/code> in the <code>string<\/code>.<\/p>\n\n\n\n<p>The result of the <code>findall()<\/code> function depends on the pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the <code>pattern<\/code> has no <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-capturing-group\/\">capturing groups<\/a>, the <code>findall()<\/code> function returns a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> of strings that match the whole pattern. <\/li><li>If the <code>pattern<\/code> has one capturing group, the <code>findall()<\/code> function returns a list of strings that match the group. <\/li><li>If the <code>pattern<\/code> has multiple capturing groups, the <code>findall()<\/code> function returns the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuples<\/a> of strings that match the groups.<\/li><\/ul>\n\n\n\n<p>It&#8217;s important to note that the non-capturing groups do not affect the form of the return result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-regex-findall-function-examples'>Python regex findall() function examples <a href=\"#python-regex-findall-function-examples\" class=\"anchor\" id=\"python-regex-findall-function-examples\" title=\"Anchor for Python regex findall() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>findall()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-python-regex-findall-to-get-a-list-of-matched-strings'>1) Using the Python regex findall() to get a list of matched strings <a href=\"#1-using-the-python-regex-findall-to-get-a-list-of-matched-strings\" class=\"anchor\" id=\"1-using-the-python-regex-findall-to-get-a-list-of-matched-strings\" title=\"Anchor for 1) Using the Python regex findall() to get a list of matched strings\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>findall()<\/code> function to get a list of color names that start with the literal string <code>bl<\/code>: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">\"black, blue and brown\"<\/span>\npattern = <span class=\"hljs-string\">r'bl\\w+'<\/span>\nmatches = re.findall(pattern,s)\n\nprint(matches)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'blue'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following pattern matches a literal string <code>bl<\/code> followed by one or more word characters specified by the <code>\\w+<\/code> rule:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-string\">'bl\\w+'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Therefore, the <code><code>findall()<\/code><\/code> function returns a list of strings that match the whole pattern. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-findall-function-with-a-pattern-that-has-a-single-group'>2) Using the findall() function with a pattern that has a single group <a href=\"#2-using-the-findall-function-with-a-pattern-that-has-a-single-group\" class=\"anchor\" id=\"2-using-the-findall-function-with-a-pattern-that-has-a-single-group\" title=\"Anchor for 2) Using the findall() function with a pattern that has a single group\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code><code>findall()<\/code><\/code> function to get a list of strings that match a group:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">\"black, blue and brown\"<\/span>\npattern = <span class=\"hljs-string\">r'bl(\\w+)'<\/span>\nmatches = re.findall(pattern,s)\n\nprint(matches)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'ack'<\/span>, <span class=\"hljs-string\">'ue'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example uses the regular expression <code>r'bl(\\w+)'<\/code> that has one capturing group <code>(\\w+)<\/code>.\n    Therefore, the <code><code>findall()<\/code><\/code> function returns a list of strings that match the group.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-findall-function-with-a-pattern-that-has-multiple-groups'>3) Using the findall() function with a pattern that has multiple groups <a href=\"#3-using-the-findall-function-with-a-pattern-that-has-multiple-groups\" class=\"anchor\" id=\"3-using-the-findall-function-with-a-pattern-that-has-multiple-groups\" title=\"Anchor for 3) Using the findall() function with a pattern that has multiple groups\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code><code>findall()<\/code><\/code> functions to get tuples of strings that match the groups in the pattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">\"black, blue and brown\"<\/span>\npattern = <span class=\"hljs-string\">r'(bl(\\w+))'<\/span>\nmatches = re.findall(pattern,s)\n\nprint(matches)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;(<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'ack'<\/span>), (<span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'ue'<\/span>)]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the pattern <code>r'(bl(\\w+))'<\/code> has two capturing groups:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>(\\w+)<\/code> captures one or more word characters.<\/li><li><code>(bl(\\w+))<\/code> captures the whole match.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='4-using-the-findall-function-with-a-regular-expression-flag'>4) Using the findall() function with a regular expression flag <a href=\"#4-using-the-findall-function-with-a-regular-expression-flag\" class=\"anchor\" id=\"4-using-the-findall-function-with-a-regular-expression-flag\" title=\"Anchor for 4) Using the findall() function with a regular expression flag\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>findall()<\/code> function with the <code>re.IGNORECASE<\/code> flag:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">\"Black, blue and brown\"<\/span>\npattern = <span class=\"hljs-string\">r'(bl(\\w+))'<\/span>\nmatches = re.findall(pattern, s, re.IGNORECASE)\n\nprint(matches)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;(<span class=\"hljs-string\">'Black'<\/span>, <span class=\"hljs-string\">'ack'<\/span>), (<span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'ue'<\/span>)]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we use the <code>re.IGNORECASE<\/code> flag in the <code>findall()<\/code> function that ignores the character cases of the matched strings. Therefore, the output includes both <code>Black<\/code> and <code>blue<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary  <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary \">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the Python regex <code>findall()<\/code> function to get a list of matched strings. <\/li><\/ul>\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=\"3278\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-findall\/\"\n\t\t\t\tdata-post-title=\"Python Regex findall()\"\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=\"3278\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-findall\/\"\n\t\t\t\tdata-post-title=\"Python Regex findall()\"\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>Summary: in this tutorial, you&#8217;ll learn how to use the Python regex findall() function to find all matches of a pattern in a string. Introduction to the Python regex findall() function # The findall() is a built-in function in the re module that handles regular expressions. The findall() function has the following syntax: In this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3122,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3278","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3278","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=3278"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3278\/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=3278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}