{"id":30588,"date":"2023-03-22T16:23:02","date_gmt":"2023-03-22T16:23:02","guid":{"rendered":"https:\/\/itslinuxfoss.com\/?p=30588"},"modified":"2023-03-22T16:23:02","modified_gmt":"2023-03-22T16:23:02","slug":"python-regex-capturing-groups","status":"publish","type":"post","link":"https:\/\/itslinuxfoss.com\/python-regex-capturing-groups\/","title":{"rendered":"Python Regex Capturing Groups"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Capturing groups can be useful for tasks like data extraction, validation, or manipulation. A regex capture group is a way to group parts of a regex pattern inside parentheses. This allows us to get only the matched parts or substring of the string, rather than the complete string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post provides an in-depth guide on Python regex capturing groups using the below contents:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"#1\">Python Regex Capturing Groups<\/a><\/strong>\n<ul class=\"wp-block-list\">\n<li><a href=\"#2\">Capturing Uppercase Word Group<\/a><\/li>\n\n\n\n<li><a href=\"#3\">Capturing Number Group<\/a><\/li>\n\n\n\n<li><a href=\"#4\">Capturing Multiple Groups<\/a><\/li>\n\n\n\n<li><a href=\"#5\">Capturing Multiple Uppercase Word Groups\u00a0<\/a><\/li>\n\n\n\n<li><a href=\"#6\">Capturing Multiple Number Groups<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1\"><strong>Python Regex Capturing Groups<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Python regex capturing group extracts parts of a string that match a specified\/particular pattern. Capturing groups can be defined by enclosing rules\/patterns inside the parentheses. For example, to capture the uppercase letters in a string like \u2018PYTHON\u2019, the pattern \u201c<strong>\\b[A-Z]+\\b<\/strong>\u201d is used inside the parentheses.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s see different examples to capture various groups from strings using Python regex:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2\"><strong>Example 1: Capturing Uppercase Word Group<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is used to capture the uppercase word groups from the given strings:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:&nbsp;<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\nstring_value = &quot;Python Guide provide by ITSLINUXFOSS&quot;\nresult = re.search(r&quot;(\\b&#x5B;A-Z]+\\b)&quot;, string_value)\nprint(result.group(1))\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The module named \u201cre\u201d is imported.<\/li>\n\n\n\n<li>The string value is initialized.<\/li>\n\n\n\n<li>The \u201cre.search()\u201d function extracts the first match for the specified pattern from the given string.<\/li>\n\n\n\n<li>The pattern r&#8221;(\\b[A-Z]+\\b)&#8221; is used inside the \u201cre.search()\u201d function. It will return one or more upper-case letters from the string.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"702\" height=\"152\" src=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1337.png\" alt=\"\" class=\"wp-image-30595\" srcset=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1337.png 702w, https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1337-300x65.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The first word in the string that matches the specified pattern has been printed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example 2: Capturing Number Group<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is used to capture the number groups from the given string:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\nstring_value = &quot;Python 2023 Guide provide by ITSLINUXFOSS&quot;\nresult = re.search(r&quot;(\\b\\d+)&quot;, string_value)\nprint(result.group(1))\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The \u201cre\u201d module is imported and the string is initialized.<\/li>\n\n\n\n<li>The pattern \u201c\\b\\d+\u201d is used to get the first matched number\/digits group from the string.<\/li>\n\n\n\n<li>The \u201cre.search()\u201d function is used to retrieve a match object if it finds a specified match, or None if it does not.<\/li>\n\n\n\n<li>The \u201cresult.group(1)\u201d returns the first group.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"591\" height=\"141\" src=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1336.png\" alt=\"\" class=\"wp-image-30594\" srcset=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1336.png 591w, https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1336-300x72.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The number that has been matched is \u201c<strong>2023<\/strong>\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4\"><strong>Example 3: Capturing Multiple Groups<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is used to capture multiple groups from the given string:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\nstring_value = &quot;Python 2023 Guide provide by ITSLINUXFOSS&quot;\noutput = re.search(r&quot;(\\b\\d+).+(\\b&#x5B;A-Z]+\\b)&quot;, string_value)\nprint(output.groups())\nprint(&#039;Capturing Numbers: &#039;,output.group(1))\nprint(&#039;Capturing Uppercase Word: &#039;,output.group(2))\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The module named \u201cre\u201d is imported at the start.<\/li>\n\n\n\n<li>The \u201cre.search()\u201d function takes the multiple patterns and finds the matched string value from the given string.<\/li>\n\n\n\n<li>The pattern \u201c(\\b\\d+)\u201d is used to capture the digits groups and the pattern \u201c(\\b[A-Z]+\\b)\u201d&nbsp; is used to capture the uppercase letters group.<\/li>\n\n\n\n<li>The \u201coutput.groups()\u201d method is used to return the captured groups as a tuple.<\/li>\n\n\n\n<li>The \u201coutput.group()\u201d function is used to display the first and second capture groups separately.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"711\" height=\"175\" src=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1335.png\" alt=\"\" class=\"wp-image-30593\" srcset=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1335.png 711w, https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1335-300x74.png 300w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The multiple patterns have been captured from the given string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5\"><strong>Example 4: Capturing Multiple Uppercase Word Groups<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following code is utilized to capture the multiple regex patterns from the given string:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\nstring_value= &quot;PYTHON guide PROVIDED by ITSLINUXFOSS&quot;\noutput= re.compile (r&quot;(\\b&#x5B;A-Z]+\\b)&quot;)\nfor m in output.finditer(string_value):\n\u00a0 \u00a0 print(m.group())\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The re module is imported and the string is initialized.<\/li>\n\n\n\n<li>The \u201cre.compile()\u201d function compiles the regular expression pattern \u201c(\\b[A-Z]+\\b)\u201d and assigns it to a variable named \u201coutput\u201d.<\/li>\n\n\n\n<li>The for loop is used to iterate all the matches of the pattern in the specified string using the \u201cfinditer()\u201d method.&nbsp;<\/li>\n\n\n\n<li>For each matching object, the function retrieves the matched substring and prints it using the \u201cgroup()\u201d method.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"607\" height=\"172\" src=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1334.png\" alt=\"\" class=\"wp-image-30592\" srcset=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1334.png 607w, https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1334-300x85.png 300w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The words in the given \u201cstring\u201d that match the pattern have been returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6\"><strong>Example 5: Capturing Multiple Number Groups&nbsp;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is used to capture the multiple groups containing digits\/numbers:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport re\nstring_value= &quot;Python Guide 2023 Provided by JOSEPH to 300 Students&quot;\noutput= re.compile (r&quot;(\\b\\d+\\b)&quot;)\nfor m in output.finditer(string_value):\n\u00a0 \u00a0 print (m.group())\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The \u201cre.compile()\u201d function compiles the regular expression pattern \u201c(\\b\\d+\\b)\u201d and assigned it to a variable named \u201coutput\u201d<\/li>\n\n\n\n<li>The for loop uses the &#8220;finditer()&#8221; method to iterate through all matches of the pattern in the specified string.<\/li>\n\n\n\n<li>The &#8220;group()&#8221; method is used to retrieve the matched substring for each matching object.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"710\" height=\"166\" src=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1333.png\" alt=\"\" class=\"wp-image-30591\" srcset=\"https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1333.png 710w, https:\/\/itslinuxfoss.com\/wp-content\/uploads\/2023\/03\/image-1333-300x70.png 300w\" sizes=\"(max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The matched digits groups have been displayed successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python regex capturing groups enable you to capture specific parts of a string based on a specified pattern such as using the (\\b\\d+) pattern for capturing digits. The \u201cregex capturing groups\u201d can be defined by placing parentheses \u201c( )\u201d around the rule\/pattern that defines or matches the specific group. To access the captured groups, we can use methods like finditer(), group(), or groups() on the match object. This guide presented various examples to capture specified groups using the Python regex module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python regex capturing groups enable you to capture specific parts of a string based on a specified pattern such as using (\\b\\d+) pattern for capturing digits.<\/p>\n","protected":false},"author":9,"featured_media":30596,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python Regex Capturing Groups - Its Linux FOSS","description":"Python regex capturing groups enable you to capture specific parts of a string based on a specified pattern such as using (\\b\\d+) pattern for capturing digits."},"footnotes":""},"categories":[321],"tags":[],"class_list":["post-30588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","infinite-scroll-item","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33","no-featured-image-padding"],"_links":{"self":[{"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/posts\/30588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/comments?post=30588"}],"version-history":[{"count":0,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/posts\/30588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/media\/30596"}],"wp:attachment":[{"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/media?parent=30588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/categories?post=30588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itslinuxfoss.com\/wp-json\/wp\/v2\/tags?post=30588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}