{"id":3318,"date":"2021-12-08T07:37:40","date_gmt":"2021-12-08T07:37:40","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3318"},"modified":"2021-12-10T01:52:42","modified_gmt":"2021-12-10T01:52:42","slug":"python-regex-finditer","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-finditer\/","title":{"rendered":"Python Regex finditer()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python regex <code>finditer()<\/code> function to find all matches in a string and return an iterator that yields match objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-regex-finditer-function'>Introduction to the Python regex finditer function <a href=\"#introduction-to-the-python-regex-finditer-function\" class=\"anchor\" id=\"introduction-to-the-python-regex-finditer-function\" title=\"Anchor for Introduction to the Python regex finditer function\">#<\/a><\/h2>\n\n\n\n<p>The <code>finditer()<\/code> function matches a pattern in a string and returns an <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterators\/\">iterator<\/a> that yields the <code>Match<\/code> objects of all non-overlapping matches.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>finditer()<\/code> function:<\/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.finditer(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 href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\">regular expression<\/a> that you want to search for in the string.<\/li><li><code>string<\/code> is the input string.<\/li><li><code>flags<\/code> parameter is optional and defaults to zero. The <code>flags<\/code> parameter accepts one or more <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-flags\/\">regex flags<\/a>. The <code>flags<\/code> parameter changes how the regex engine matches the pattern.<\/li><\/ul>\n\n\n\n<p>If the search is successful, the <code>finditer()<\/code> function returns an iterator yielding the <code>Match<\/code> objects. Otherwise, the <code>finditer()<\/code> also returns an iterator that will yield no <code>Match<\/code> object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-regex-finditer-example'>Python regex finditer example <a href=\"#python-regex-finditer-example\" class=\"anchor\" id=\"python-regex-finditer-example\" title=\"Anchor for Python regex finditer example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>finditer()<\/code> function to search for all vowels in a string:<\/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\">'Readability counts.'<\/span>\npattern = <span class=\"hljs-string\">r'&#91;aeoui]'<\/span>\n\nmatches = re.finditer(pattern, s)\n<span class=\"hljs-keyword\">for<\/span> match <span class=\"hljs-keyword\">in<\/span> matches:\n    print(match)<\/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\">&lt;re.Match object; span=(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>), match=<span class=\"hljs-string\">'e'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>), match=<span class=\"hljs-string\">'a'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>), match=<span class=\"hljs-string\">'a'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>), match=<span class=\"hljs-string\">'i'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>), match=<span class=\"hljs-string\">'i'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">13<\/span>, <span class=\"hljs-number\">14<\/span>), match=<span class=\"hljs-string\">'o'<\/span>&gt;\n&lt;re.Match object; span=(<span class=\"hljs-number\">14<\/span>, <span class=\"hljs-number\">15<\/span>), match=<span class=\"hljs-string\">'u'<\/span>&gt;<\/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<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 <code>finditer()<\/code> function to match a pattern in a string and return an iterator yielding the Match objects.<\/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=\"3318\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-finditer\/\"\n\t\t\t\tdata-post-title=\"Python Regex finditer()\"\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=\"3318\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-finditer\/\"\n\t\t\t\tdata-post-title=\"Python Regex finditer()\"\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 tutorial, you&#8217;ll learn how to use the Python regex finditer() function to find all matches in a string and return an iterator that yields match objects.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3122,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3318","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3318","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=3318"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3318\/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=3318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}