{"id":348,"date":"2024-01-28T11:35:40","date_gmt":"2024-01-28T11:35:40","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=348"},"modified":"2024-01-28T11:35:41","modified_gmt":"2024-01-28T11:35:41","slug":"python-regex-quantifiers","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/28\/python-regex-quantifiers\/","title":{"rendered":"Python Regex Quantifiers"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use Python regex quantifiers to define how many times a character or a character set can be repeated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python regex quantifiers<\/h2>\n\n\n\n<p>In\u00a0regular expressions, quantifiers match the preceding characters or character sets a number of times. The following table shows all the quantifiers and their meanings:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Quantifier<\/th><th>Name<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>*<\/code><\/td><td>Asterisk<\/td><td>Match its preceding element zero or more times.<\/td><\/tr><tr><td><code>+<\/code><\/td><td>Plus<\/td><td>Match its preceding element one or more times.<\/td><\/tr><tr><td><code>?<\/code><\/td><td>Question Mark<\/td><td>Match its preceding element zero or one time.<\/td><\/tr><tr><td><code>{<\/code>&nbsp;<em>n<\/em>&nbsp;<code>}<\/code><\/td><td>Curly Braces<\/td><td>Match its preceding element exactly&nbsp;<code>n<\/code>&nbsp;times.<\/td><\/tr><tr><td><code>{<\/code>&nbsp;<em>n<\/em>&nbsp;<code>,}<\/code><\/td><td>Curly Braces<\/td><td>Match its preceding element at least&nbsp;<code>n<\/code>&nbsp;times.<\/td><\/tr><tr><td><code>{<\/code>&nbsp;<em>n<\/em>&nbsp;<code>,<\/code>&nbsp;<em>m<\/em>&nbsp;<code>}<\/code><\/td><td>Curly Braces<\/td><td>Match its preceding element from&nbsp;<code>n<\/code>&nbsp;to&nbsp;<code>m<\/code>&nbsp;times.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Match zero or more times (<code>*<\/code>)<\/h3>\n\n\n\n<p>The quantifier (<code>*<\/code>) matches its preceding element zero or more times. For example, the following program uses the\u00a0<code>*<\/code>\u00a0quantifier to match any string that ends with\u00a0<code>Python<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"\"\"CPython, IronPython, and JPython are major Python's implementation\"\"\" matches = re.finditer('\\w*Python', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>\\w<\/code>&nbsp;matches any single word character.<\/li>\n\n\n\n<li>So the&nbsp;<code>\\w*<\/code>&nbsp;matches zero or more word characters.<\/li>\n\n\n\n<li>Therefore, the&nbsp;<code>\\w*Python<\/code>&nbsp;match any zero or more characters followed by the string&nbsp;<code>Python<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>As a result, the\u00a0<code>\\w*Python<\/code>\u00a0pattern matches\u00a0<code>CPython<\/code>,\u00a0<code>IronPython<\/code>,\u00a0<code>JPython<\/code>, and\u00a0<code>Python<\/code>\u00a0in the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(0, 7), match='CPython'> &lt;re.Match object; span=(9, 19), match='IronPython'> &lt;re.Match object; span=(25, 32), match='JPython'> &lt;re.Match object; span=(51, 57), match='Python'> <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Match one or more times (<code>+<\/code>)<\/h3>\n\n\n\n<p>The&nbsp;<code>+<\/code>&nbsp;quantifier matches its preceding element one or more times. For example, the&nbsp;<code>\\d+<\/code>&nbsp;matches one or more digits.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>+<\/code>\u00a0quantifier to match one or more digits in a string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"Python 3.10 was released in 2021\" matches = re.finditer('\\d+', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(7, 8), match='3'> &lt;re.Match object; span=(9, 11), match='10'> &lt;re.Match object; span=(28, 32), match='2021'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Match zero or one time (<code>?<\/code>)<\/h3>\n\n\n\n<p>The&nbsp;<code>?<\/code>&nbsp;quantifier matches its preceding element zero or one time.<\/p>\n\n\n\n<p>The following example uses the (?) quantifier to match both strings\u00a0<code>color<\/code>\u00a0and\u00a0<code>colour<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"What color \/ colour do you like?\" matches = re.finditer('colou?r', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(5, 10), match='color'> &lt;re.Match object; span=(13, 19), match='colour'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>u?<\/code>&nbsp;matches zero or one character&nbsp;<code>u<\/code>. Therefore, the&nbsp;<code>colou?r<\/code>&nbsp;pattern matches both&nbsp;<code>color<\/code>&nbsp;and&nbsp;<code>colour<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Match Exactly n Times: {n}<\/h3>\n\n\n\n<p>The&nbsp;<code>{n}<\/code>&nbsp;quantifier matches its preceding element exactly&nbsp;<code>n<\/code>&nbsp;times, where&nbsp;<code>n<\/code>&nbsp;is zero or a positive integer.<\/p>\n\n\n\n<p>For example, the following program uses the quantifier\u00a0<code>{n}<\/code>\u00a0to match a time string in the\u00a0<code>hh:mm<\/code>\u00a0format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"It was 11:05 AM\" matches = re.finditer('\\d{2}:\\d{2}', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(7, 12), match='11:05'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>\\d{2}<\/code>&nbsp;matches exactly two digits. Therefore, the&nbsp;<code>\\d{2}:\\d{2}<\/code>&nbsp;matches a string that starts with two digits, a colon&nbsp;<code>:<\/code>, and ends with two digits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Match at least n times: {n,}<\/h3>\n\n\n\n<p>The&nbsp;<code>{n,}<\/code>&nbsp;quantifier matches its preceding element at least&nbsp;<code>n<\/code>&nbsp;times, where&nbsp;<code>n<\/code>&nbsp;is zero or a positive integer.<\/p>\n\n\n\n<p>For example, the following program uses the\u00a0<code>{n, }<\/code>\u00a0quantifier to match the date strings with the\u00a0<code>m-d-yyyy<\/code>\u00a0or\u00a0<code>mm-dd-yyyy<\/code>\u00a0format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"5-5-2021 or 05-05-2021 or 5\/5\/2021\" matches = re.finditer('\\d{1,}-\\d{1,}-\\d{4}', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(0, 8), match='5-5-2021'> &lt;re.Match object; span=(12, 22), match='05-05-2021'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Match from n and m times: {n,m}<\/h3>\n\n\n\n<p>The\u00a0<code>{n,m}<\/code>\u00a0quantifier matches its preceding element at least\u00a0<code>n<\/code>\u00a0times, but no more than\u00a0<code>m<\/code>\u00a0times, where\u00a0<code>n<\/code>\u00a0and\u00a0<code>m<\/code>\u00a0are zero or a positive integer. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import re s = \"5-5-2021 or 05-05-2021 or 5\/5\/2021\" matches = re.finditer('\\d{1,2}-\\d{1,2}-\\d{4}', s) for match in matches: print(match)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;re.Match object; span=(0, 8), match='5-5-2021'> &lt;re.Match object; span=(12, 22), match='05-05-2021'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the pattern&nbsp;<code>\\d{1,2}<\/code>&nbsp;matches one or two digits. So the pattern&nbsp;<code>\\d{1,2}-\\d{1,2}-\\d{4}<\/code>&nbsp;matches a date string in the&nbsp;<code>d-m-yyyy<\/code>&nbsp;or&nbsp;<code>dd-mm-yyyy<\/code>&nbsp;format.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use Python regex quantifiers to define how many times a character or a character set can be repeated. Introduction to Python regex quantifiers In\u00a0regular expressions, quantifiers match the preceding characters or character sets a number of times. The following table shows all the quantifiers and their meanings: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-2-python-regex"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=348"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/348\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/348\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}