{"id":132888,"date":"2023-04-03T13:25:07","date_gmt":"2023-04-03T13:25:07","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1264127"},"modified":"2023-04-03T13:25:07","modified_gmt":"2023-04-03T13:25:07","slug":"how-to-access-multiple-matches-of-a-regex-group-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/04\/03\/how-to-access-multiple-matches-of-a-regex-group-in-python\/","title":{"rendered":"How to Access Multiple Matches of a Regex Group in Python?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1264127&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Access Multiple Matches of a Regex Group in Python?&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>In this article, I will cover <strong><em>accessing multiple matches of a regex group in Python<\/em><\/strong>. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex\/\" data-type=\"post\" data-id=\"6210\" target=\"_blank\">Regular expressions (regex)<\/a><\/strong> are a powerful tool for text processing and pattern matching, making it easier to work with strings. When working with regular expressions in Python, we often need to access <em>multiple matches<\/em> of a single regex group. This can be particularly useful when parsing large amounts of text or extracting specific information from a string.<\/p>\n<p>To access multiple matches of a regex group in Python, you can use the <strong><code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex-finditer\/\" data-type=\"post\" data-id=\"17635\" target=\"_blank\">re.finditer()<\/a><\/code><\/strong> or the <code><strong><a href=\"https:\/\/blog.finxter.com\/python-re-findall\/\" data-type=\"post\" data-id=\"5729\" target=\"_blank\" rel=\"noreferrer noopener\">re.findall()<\/a><\/strong><\/code> method. <\/p>\n<ul>\n<li>The <code>re.finditer()<\/code> method finds all matches and returns an <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/iterators-iterables-and-itertools\/\" data-type=\"post\" data-id=\"29507\" target=\"_blank\">iterator<\/a> yielding match objects that match the regex pattern. Next, you can iterate over each match object and extract its value. <\/li>\n<li>The <code>re.findall()<\/code> method returns all matches in a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list<\/a>, which can be a more convenient option if you want to work with lists directly.<\/li>\n<\/ul>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f469-200d-1f4bb.png\" alt=\"\ud83d\udc69\u200d\ud83d\udcbb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Problem Formulation<\/strong>: Given a regex pattern and a text string, how can you access multiple matches of a regex group in Python? <\/p>\n<h2 class=\"wp-block-heading\">Understanding Regex in Python<\/h2>\n<p>In this section, I&#8217;ll introduce you to the basics of regular expressions and how we can work with them in Python using the &#8216;<code>re<\/code>&#8216; module. So, buckle up, and let&#8217;s get started! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f604.png\" alt=\"\ud83d\ude04\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Basics of Regular Expressions<\/h3>\n<p>Regular expressions are sequences of characters that define a search pattern. These patterns can match strings or perform various operations like search, replace, and split into text data. <\/p>\n<p>Some common regex elements include:<\/p>\n<ul>\n<li><strong>Literals:<\/strong> Regular characters like <code>'a'<\/code>, <code>'b'<\/code>, or <code>'1'<\/code> that match themselves.<\/li>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/regex-special-characters-examples-in-python-re\/\" data-type=\"post\" data-id=\"6421\" target=\"_blank\" rel=\"noreferrer noopener\">Metacharacters<\/a>:<\/strong> Special characters like <code>'.'<\/code>, <code>'*'<\/code>, or <code>'+'<\/code> that have a special meaning in regex.<\/li>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/python-character-set-regex-tutorial\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-character-set-regex-tutorial\/\">Character classes<\/a>:<\/strong> A set of characters enclosed in square brackets (e.g., <code>'[a-z]'<\/code> or <code>'[0-9]'<\/code>).<\/li>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/python-regex-quantifiers-question-mark-vs-plus-vs-asterisk-differences\/\" data-type=\"post\" data-id=\"6915\" target=\"_blank\" rel=\"noreferrer noopener\">Quantifiers<\/a>:<\/strong> Specify how many times an element should repeat (e.g., <code>'{3}'<\/code>, <code>'{2,5}'<\/code>, or <code>'?'<\/code>).<\/li>\n<\/ul>\n<p>These elements can be combined to create complex search patterns. For example, the pattern <code>'\\d{3}-\\d{2}-\\d{4}'<\/code> would match a string like <code>'123-45-6789'<\/code>. <\/p>\n<p>Remember, practice makes perfect, and the more you work with regex, the more powerful your text processing skills will become.<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4aa.png\" alt=\"\ud83d\udcaa\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">The Python &#8216;re&#8217; Module<\/h3>\n<p>Python comes with a built-in module called &#8216;<code>re<\/code>&#8216; that makes it easy to work with regular expressions. To start using regex in Python, simply import the &#8216;<code>re<\/code>&#8216; module like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re<\/pre>\n<p>Once imported, the &#8216;<code>re<\/code>&#8216; module provides several useful functions for working with regex, such as:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<th>Function<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code><a href=\"https:\/\/academy.finxter.com\/course\/python-regex-match-a-complete-guide-to-re-match\/\" target=\"_blank\" rel=\"noreferrer noopener\">re.match()<\/a><\/code><\/td>\n<td>Checks if a regex pattern matches at the beginning of a string.<\/td>\n<\/tr>\n<tr>\n<td><code><a href=\"https:\/\/blog.finxter.com\/python-regex-search\/\" data-type=\"post\" data-id=\"5749\" target=\"_blank\" rel=\"noreferrer noopener\">re.search()<\/a><\/code><\/td>\n<td>Searches for a regex pattern in a string and returns a match object if found.<\/td>\n<\/tr>\n<tr>\n<td><code><a href=\"https:\/\/blog.finxter.com\/python-re-findall\/\" data-type=\"post\" data-id=\"5729\" target=\"_blank\" rel=\"noreferrer noopener\">re.findall()<\/a><\/code><\/td>\n<td>Returns all non-overlapping matches of a regex pattern in a string as a list.<\/td>\n<\/tr>\n<tr>\n<td><code><a href=\"https:\/\/blog.finxter.com\/python-regex-finditer\/\" data-type=\"post\" data-id=\"17635\" target=\"_blank\" rel=\"noreferrer noopener\">re.finditer()<\/a><\/code><\/td>\n<td>Returns an iterator yielding match objects for all non-overlapping matches of a regex pattern in a string.<\/td>\n<\/tr>\n<tr>\n<td><code><a href=\"https:\/\/academy.finxter.com\/course\/python-regex-sub-how-to-replace-a-pattern-in-a-string\/\" data-type=\"URL\" data-id=\"https:\/\/academy.finxter.com\/course\/python-regex-sub-how-to-replace-a-pattern-in-a-string\/\" target=\"_blank\" rel=\"noreferrer noopener\">re.sub()<\/a><\/code><\/td>\n<td>Replaces all occurrences of a regex pattern in a string with a specified substitution.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>By using these functions provided by the &#8216;<code>re<\/code>&#8216; module, we can harness the full power of regular expressions in our Python programs. So, let&#8217;s dive in and start matching! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Working with Regex Groups<\/h2>\n<p>When working with regular expressions in Python, it&#8217;s common to encounter situations where we need to access multiple matches of a <a href=\"https:\/\/blog.finxter.com\/python-regex-named-groups\/\" data-type=\"post\" data-id=\"836544\" target=\"_blank\" rel=\"noreferrer noopener\">regex group<\/a>. In this section, I&#8217;ll guide you through defining and capturing regex groups, creating a powerful tool to manipulate text data. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f604.png\" alt=\"\ud83d\ude04\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Defining Groups<\/h3>\n<p>First, let&#8217;s talk about how to define groups within a regular expression. To create a group, simply enclose the part of the pattern you want to capture in parentheses. For example, if I want to match and capture a sequence of uppercase letters, I would use the pattern <code>([A-Z]+)<\/code>. The parentheses tell Python that everything inside should be treated as a single group. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4da.png\" alt=\"\ud83d\udcda\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Now, let&#8217;s say I want to find multiple groups of uppercase letters, separated by commas. In this case, I can use the pattern <code>([A-Z]+),?([A-Z]+)?<\/code>. With this pattern, I&#8217;m telling Python to look for one or two groups of <a href=\"https:\/\/blog.finxter.com\/python-convert-string-list-to-uppercase\/\" data-type=\"post\" data-id=\"814661\" target=\"_blank\" rel=\"noreferrer noopener\">uppercase<\/a> letters, with an optional comma in between. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Capturing Groups<\/h3>\n<p>To access the matches of the defined groups, Python provides a few helpful functions in its <code>re<\/code> module. One such function is <code>findall()<\/code>, which returns a list of all non-overlapping matches in the string<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f50d.png\" alt=\"\ud83d\udd0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>. <\/p>\n<p>For example, using our previous pattern:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\npattern = r'([A-Z]+),?([A-Z]+)?'\ntext = \"HELLO,WORLD,HOW,AREYOU\"\nmatches = re.findall(pattern, text)\nprint(matches)\n<\/pre>\n<p>This code would return the following result: <\/p>\n<p><code>[('HELLO', 'WORLD'), ('HOW', ''), ('ARE', 'YOU')]<\/code><\/p>\n<p>Notice how it returns a list of tuples, with each <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\" rel=\"noreferrer noopener\">tuple<\/a> containing the matches for the specified groups. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f60a.png\" alt=\"\ud83d\ude0a\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-global-color-8-background-color has-background\">Another useful function is <code>finditer()<\/code>, which returns an iterator yielding <code>Match<\/code> objects matching the regex pattern. To extract the group values, simply call the <code>group()<\/code> method on the <code>Match<\/code> object, specifying the index of the group we&#8217;re interested in.<\/p>\n<p>An example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re\npattern = r'([A-Z]+),?([A-Z]+)?'\ntext = \"HELLO,WORLD,HOW,AREYOU\" for match in re.finditer(pattern, text): print(\"Group 1:\", match.group(1)) print(\"Group 2:\", match.group(2))\n<\/pre>\n<p>This code would output the following:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Group 1: HELLO\nGroup 2: WORLD\nGroup 1: HOW\nGroup 2:\nGroup 1: ARE\nGroup 2: YOU\n<\/pre>\n<p>As you can see, using regex groups in Python offers a flexible and efficient way to deal with pattern matching and text manipulation. I hope this helps you on your journey to becoming a regex master! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f31f.png\" alt=\"\ud83c\udf1f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Accessing Multiple Matches<\/h2>\n<p>As a Python user, sometimes I need to find and capture multiple matches of a regex group in a string. This can seem tricky, but there are two convenient functions to make this task a lot easier: <code>finditer<\/code> and <code>findall<\/code>.<\/p>\n<h3 class=\"wp-block-heading\">Using &#8216;finditer&#8217; Function<\/h3>\n<p>I often use the <code>finditer<\/code> function when I want to access multiple matches within a group. It finds all matches and returns an iterator, yielding match objects that correspond with the regex pattern <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f9e9.png\" alt=\"\ud83e\udde9\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>. <\/p>\n<p>To extract the values from the match objects, I simply need to iterate through each object <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f504.png\" alt=\"\ud83d\udd04\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re pattern = re.compile(r'your_pattern')\nmatches = pattern.finditer(your_string) for match in matches: print(match.group())\n<\/pre>\n<p>This useful method allows me to get all the matches without any hassle. You can find more about this method in <a href=\"https:\/\/pynative.com\/python-regex-capturing-groups\/\">PYnative&#8217;s tutorial<\/a> on Python regex capturing groups.<\/p>\n<h3 class=\"wp-block-heading\">Using &#8216;findall&#8217; Function<\/h3>\n<p>Another option I consider when searching for multiple matches in a group is the <code>findall<\/code> function. It returns a list containing all matches&#8217; strings. Unlike <code>finditer<\/code>, <code>findall<\/code> doesn&#8217;t return match objects, so the result is directly usable as a list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re pattern = re.compile(r'your_pattern')\nall_matches = pattern.findall(your_string) print(all_matches)\n<\/pre>\n<p>This method provides me with a simple way to access <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2699.png\" alt=\"\u2699\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> all the matches as strings in a list.<\/p>\n<h2 class=\"wp-block-heading\">Practical Examples<\/h2>\n<p>Let&#8217;s dive into some hands-on examples of how to access multiple matches of a regex group in Python. These examples will demonstrate how versatile and powerful regular expressions can be when it comes to text processing.<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Extracting Email Addresses<\/h3>\n<p>Suppose I want to extract all email addresses from a given text. Here&#8217;s how I&#8217;d do it using Python regex:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re text = \"Contact me at [email&amp;nbsp;protected] and my friend at [email&amp;nbsp;protected]\"\npattern = r'([\\w\\.-]+)@([\\w\\.-]+)\\.(\\w+)'\nmatches = re.findall(pattern, text) for match in matches: email = f\"{match[0]}@{match[1]}.{match[2]}\" print(f\"Found email: {email}\")\n<\/pre>\n<p>This code snippet extracts email addresses by using a regex pattern that has three capturing groups. The <code>re.findall()<\/code> function returns a list of tuples, where each tuple contains the text matched by each group. I then reconstruct email addresses from the extracted text using string formatting.<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f44c.png\" alt=\"\ud83d\udc4c\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Finding Repeated Words<\/h3>\n<p>Now, let&#8217;s say I want to find all repeated words in a text. Here&#8217;s how I can achieve this with Python regex:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import re text = \"I saw the cat and the cat was sleeping near the the door\"\npattern = r'\\b(\\w+)\\b\\s+\\1\\b'\nmatches = re.findall(pattern, text, re.IGNORECASE) for match in matches: print(f\"Found repeated word: {match}\")\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Found repeated word: the<\/code><\/pre>\n<\/p>\n<p>In this example, I use a regex pattern with a single capturing group to match words (using the <code>\\b<\/code> word boundary anchor). The <code>\\1<\/code> syntax refers to the text matched by the first group, allowing us to find consecutive occurrences of the same word. The <code>re.IGNORECASE<\/code> flag ensures case-insensitive matching. So, no repeated word can escape my Python regex magic!<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2728.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>In this article, I discussed how to access multiple matches of a regex group in Python. I found that using the <code>finditer()<\/code> method is a powerful way to achieve this goal. By leveraging this method, I can easily iterate through all match objects and extract the values I need. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f603.png\" alt=\"\ud83d\ude03\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Along the way, I learned that <code>finditer()<\/code> returns an iterator yielding match objects, which allows for greater flexibility when working with regular expressions in Python. I can efficiently process these match objects and extract important information for further manipulation and analysis. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f469-200d-1f4bb.png\" alt=\"\ud83d\udc69\u200d\ud83d\udcbb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<div class=\"wp-block-group\">\n<div class=\"wp-block-group__inner-container is-layout-flow\">\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\">Python Regex Course<\/a><\/h2>\n<p><strong><em>Google engineers are regular expression masters. <\/em><\/strong>The Google search engine is a massive <em>text-processing engine<\/em> that extracts value from trillions of webpages.\u00a0\u00a0<\/p>\n<p><strong><em>Facebook engineers are regular expression masters.<\/em><\/strong> Social networks like Facebook, WhatsApp, and Instagram connect humans via <em>text messages<\/em>.\u00a0<\/p>\n<p><strong><em>Amazon engineers are regular expression masters. <\/em><\/strong>Ecommerce giants ship products based on <em>textual product descriptions<\/em>.\u00a0\u00a0Regular expressions \u200brule the game \u200bwhen text processing \u200bmeets computer science.\u00a0<\/p>\n<p><em><strong>If you want to become a regular expression master too, check out the<a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\"> most comprehensive Python regex course<\/a> on the planet:<\/strong><\/em><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/academy.finxter.com\/university\/mastering-regular-expressions\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-1024x576.jpg\" alt=\"\" class=\"wp-image-19840\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-1536x864.jpg 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-2048x1152.jpg 2048w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2018\/10\/ClickToPlay-150x84.jpg 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) In this article, I will cover accessing multiple matches of a regex group in Python. Regular expressions (regex) are a powerful tool for text processing and pattern matching, making it easier to work with strings. When working with regular expressions in Python, we often need to access multiple matches of a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-132888","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/132888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=132888"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/132888\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=132888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=132888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=132888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}