{"id":3124,"date":"2021-11-29T07:55:07","date_gmt":"2021-11-29T07:55:07","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3124"},"modified":"2021-12-09T10:12:46","modified_gmt":"2021-12-09T10:12:46","slug":"python-regular-expressions","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/","title":{"rendered":"Python Regular Expressions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python regular expressions and how to use the most commonly used regular expression functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-regular-expressions'>Introduction to the Python regular expressions <a href=\"#introduction-to-the-python-regular-expressions\" class=\"anchor\" id=\"introduction-to-the-python-regular-expressions\" title=\"Anchor for Introduction to the Python regular expressions\">#<\/a><\/h2>\n\n\n\n<p>Regular expressions (called regex or regexp) specify search patterns. Typical examples of regular expressions are the patterns for matching email addresses, phone numbers, and credit card numbers.<\/p>\n\n\n\n<p>Regular expressions are essentially a specialized programming language embedded in Python. And you can interact with regular expressions via the built-in <code>re<\/code> module in Python.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/11\/python-regular-expressions.svg\" alt=\"\" class=\"wp-image-3131\"\/><\/figure><\/div>\n\n\n\n<p>The following shows an example of a simple regular expression:<\/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\"><span class=\"hljs-string\">'\\d'<\/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 example, a regular expression is a string that contains a search pattern. The <code>'\\d'<\/code> is a digit <a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-character-set\/\">character set<\/a> that matches any single digit from 0 to 9. <\/p>\n\n\n\n<p class=\"note\">Note that you&#8217;ll learn how to construct more complex and advanced patterns in the next tutorials. This tutorial focuses on the functions that deal with regular expressions.<\/p>\n\n\n\n<p>To use this regular expression, you follow these steps:<\/p>\n\n\n\n<p>First, import the <code>re<\/code> module:<\/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<\/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>Second, compile the regular expression into a <code>Pattern<\/code> object:<\/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\">p = re.compile(<span class=\"hljs-string\">'\\d'<\/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>Third, use one of the methods of the <code>Pattern<\/code> object to match a string:<\/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\">s = <span class=\"hljs-string\">\"Python 3.10 was released on October 04, 2021\"<\/span>\nresult = p.findall(s)\n\nprint(result)<\/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>Output:<\/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\">&#91;<span class=\"hljs-string\">'3'<\/span>, <span class=\"hljs-string\">'1'<\/span>, <span class=\"hljs-string\">'0'<\/span>, <span class=\"hljs-string\">'0'<\/span>, <span class=\"hljs-string\">'4'<\/span>, <span class=\"hljs-string\">'2'<\/span>, <span class=\"hljs-string\">'0'<\/span>, <span class=\"hljs-string\">'2'<\/span>, <span class=\"hljs-string\">'1'<\/span>]<\/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>The <code>findall()<\/code> method returns a list of single digits in the string s. <\/p>\n\n\n\n<p>The following shows the complete program:<\/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\"><span class=\"hljs-keyword\">import<\/span> re\n\n\np = re.compile(<span class=\"hljs-string\">'\\d'<\/span>)\ns = <span class=\"hljs-string\">\"Python 3.10 was released on October 04, 2021\"<\/span>\n\nresults = p.findall(s)\nprint(results)<\/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>Besides the <code>findall()<\/code> method, the <code>Pattern<\/code> object has other essential methods that allow you to match a string:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code><a href=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regex-match\/\">match()<\/a><\/code><\/td><td>Find the pattern at the beginning of a string<\/td><\/tr><tr><td><code>search()<\/code><\/td><td>Return the first match of a pattern in a string<\/td><\/tr><tr><td><code>findall()<\/code><\/td><td>Return all matches of a pattern in a string<\/td><\/tr><tr><td><code>finditer()<\/code><\/td><td>Return all matches of a pattern as an <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterators\/\">iterator<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-regular-expression-functions'>Python regular expression functions <a href=\"#python-regular-expression-functions\" class=\"anchor\" id=\"python-regular-expression-functions\" title=\"Anchor for Python regular expression functions\">#<\/a><\/h2>\n\n\n\n<p>Besides the <code>Pattern<\/code> class, the <code>re<\/code> module has some functions that match a string for a pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>match()<\/code><\/li><li><code>search()<\/code><\/li><li><code>findall()<\/code><\/li><li><code>finditer()<\/code><\/li><\/ul>\n\n\n\n<p>These functions have the same names as the methods of the <code>Pattern<\/code> object. Also, they take the same arguments as the corresponding methods of the <code>Pattern<\/code> object. However, you don&#8217;t have to manually compile the regular expression before using it.<\/p>\n\n\n\n<p>The following example shows the same program that uses the <code>findall()<\/code> function instead of the <code>findall()<\/code> method of a <code>Pattern<\/code> object:<\/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\">\"Python 3.10 was released on October 04, 2021.\"<\/span>\nresults = re.findall(<span class=\"hljs-string\">'\\d'<\/span>,s)\nprint(results)<\/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>Using the functions in the <code>re<\/code> module is more concise than the methods of the <code>Pattern<\/code> object because you don&#8217;t have to compile regular expressions manually.<\/p>\n\n\n\n<p>Under the hood, these functions create a <code>Pattern<\/code> object and call the appropriate method on it. They also store the compiled regular expression in a cache for speed optimization. <\/p>\n\n\n\n<p>It means that if you call the same regular expression from the second time, these functions will not need to recompile the regular expression. Instead, they get the compiled regular expression from the cache.<\/p>\n\n\n\n<p>Should you use the <code>re<\/code> functions or methods of the <code>Pattern<\/code> object?<\/p>\n\n\n\n<p>If you use a regular expression within a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-range\/\">loop<\/a>, the <code>Pattern<\/code> object may save a few function calls. However, if you use it outside of loops, the difference is very little due to the internal cache.<\/p>\n\n\n\n<p>The following sections discuss the most commonly used functions in the <code>re<\/code> module including <code>search()<\/code>, <code>match()<\/code>, and <code>fullmatch()<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='search-function'>search() function <a href=\"#search-function\" class=\"anchor\" id=\"search-function\" title=\"Anchor for search() function\">#<\/a><\/h3>\n\n\n\n<p>The <code>search()<\/code> function searches for a pattern within a string. If there is a match, it returns the first Match object or None otherwise. For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">\"Python 3.10 was released on October 04, 2021.\"<\/span>\n\npattern = <span class=\"hljs-string\">'\\d{2}'<\/span>\nmatch = re.search(pattern, s) \nprint(type(match))\nprint(match)<\/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>Output:<\/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\">&lt;<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> '<span class=\"hljs-title\">re<\/span>.<span class=\"hljs-title\">Match<\/span>'&gt;\n&lt;<span class=\"hljs-title\">re<\/span>.<span class=\"hljs-title\">Match<\/span> <span class=\"hljs-title\">object<\/span>; <span class=\"hljs-title\">span<\/span>=<span class=\"hljs-params\">(<span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">11<\/span>)<\/span>, <span class=\"hljs-title\">match<\/span>='10'&gt;<\/span><\/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>In this example, the <code>search()<\/code> function returns the first two digits in the string <code>s<\/code> as the <code>Match<\/code> object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='match-object'>Match object <a href=\"#match-object\" class=\"anchor\" id=\"match-object\" title=\"Anchor for Match object\">#<\/a><\/h3>\n\n\n\n<p>The <code>Match<\/code> object provides the information about the matched string. It has the following important methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>group()<\/code><\/td><td>Return the matched string<\/td><\/tr><tr><td><code>start()<\/code><\/td><td>Return the starting position of the match<\/td><\/tr><tr><td><code>end()<\/code><\/td><td>Return the ending position of the match<\/td><\/tr><tr><td><code>span()<\/code><\/td><td>Return a tuple (start, end) that specifies the positions of the match<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example examines the <code>Match<\/code> object:<\/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\"><span class=\"hljs-keyword\">import<\/span> re\n\n\ns = <span class=\"hljs-string\">\"Python 3.10 was released on October 04, 2021.\"<\/span>\nresult = re.search(<span class=\"hljs-string\">'\\d'<\/span>, s) \n\nprint(<span class=\"hljs-string\">'Matched string:'<\/span>,result.group())\nprint(<span class=\"hljs-string\">'Starting position:'<\/span>, result.start())\nprint(<span class=\"hljs-string\">'Ending position:'<\/span>,result.end())\nprint(<span class=\"hljs-string\">'Positions:'<\/span>,result.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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Matched string: <span class=\"hljs-number\">3<\/span>\nStarting position: <span class=\"hljs-number\">7<\/span>\nEnding position: <span class=\"hljs-number\">8<\/span>\nPositions: (<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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<h3 class=\"wp-block-heading\" id='match-function'>match() function <a href=\"#match-function\" class=\"anchor\" id=\"match-function\" title=\"Anchor for match() function\">#<\/a><\/h3>\n\n\n\n<p>The match() function returns a <code>Match<\/code> object if it finds a pattern at the beginning of a string. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> re\n\nl = &#91;<span class=\"hljs-string\">'Python'<\/span>, \n    <span class=\"hljs-string\">'CPython is an implementation of Python written in C'<\/span>, \n    <span class=\"hljs-string\">'Jython is a Java implementation of Python'<\/span>,\n     <span class=\"hljs-string\">'IronPython is Python on .NET framework'<\/span>]\n\npattern = <span class=\"hljs-string\">'\\wython'<\/span>\n<span class=\"hljs-keyword\">for<\/span> s <span class=\"hljs-keyword\">in<\/span> l:\n    result = re.match(pattern,s)\n    print(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><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-13\" 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\">0<\/span>, <span class=\"hljs-number\">6<\/span>), match=<span class=\"hljs-string\">'Python'<\/span>&gt;\n<span class=\"hljs-literal\">None<\/span>\n&lt;re.Match object; span=(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">6<\/span>), match=<span class=\"hljs-string\">'Jython'<\/span>&gt;\n<span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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 <code>\\w<\/code> is the word character set that matches any single character. <\/p>\n\n\n\n<p>The <code>\\wython<\/code> matches any string that starts with any sing word character and is followed by the literal string <code>ython<\/code>, for example, <code>Python<\/code>.<\/p>\n\n\n\n<p>Since the <code>match()<\/code> function only finds the pattern at the beginning of a string, the following strings match the pattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Python\nJython <span class=\"hljs-keyword\">is<\/span> a Java implementation of Python<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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>And the following string doesn&#8217;t match:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-string\">'CPython is an implementation of Python written in C'<\/span>\n<span class=\"hljs-string\">'IronPython is Python on .NET framework'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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<h3 class=\"wp-block-heading\" id='fullmatch-function'>fullmatch() function <a href=\"#fullmatch-function\" class=\"anchor\" id=\"fullmatch-function\" title=\"Anchor for fullmatch() function\">#<\/a><\/h3>\n\n\n\n<p>The <code>fullmatch()<\/code> function returns a <code>Match<\/code> object if the whole string matches a pattern or <code>None<\/code> otherwise. The following example uses the <code>fullmatch()<\/code> function to match a string with four digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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\">\"2021\"<\/span>\npattern = <span class=\"hljs-string\">'\\d{4}'<\/span>\nresult = re.fullmatch(pattern, s)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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-17\" 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\">0<\/span>, <span class=\"hljs-number\">4<\/span>), match=<span class=\"hljs-string\">'2019'<\/span>&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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 pattern <code>'\\d{4}'<\/code> matches a string with four digits. Therefore, the <code>fullmatch()<\/code> function returns the string <code>2021<\/code>. <\/p>\n\n\n\n<p>If you place the number <code>2021<\/code> at the middle or the end of the string, the <code>fullmatch()<\/code> will return <code>None<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" 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\">\"Python 3.10 released in 2021\"<\/span>\npattern = <span class=\"hljs-string\">'\\d{4}'<\/span>\nresult = re.fullmatch(pattern, s)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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='regular-expressions-and-raw-strings'>Regular expressions and raw strings <a href=\"#regular-expressions-and-raw-strings\" class=\"anchor\" id=\"regular-expressions-and-raw-strings\" title=\"Anchor for Regular expressions and raw strings\">#<\/a><\/h2>\n\n\n\n<p>It&#8217;s important to note that Python and regular expression are different programming languages. They have their own syntaxes.<\/p>\n\n\n\n<p>The <code>re<\/code> module is the interface between Python and regular expression programming languages. It behaves like an interpreter between them.<\/p>\n\n\n\n<p>To construct a pattern, regular expressions often use a backslash <code>'\\'<\/code> for example <code>\\d<\/code> and <code>\\w<\/code> . But this collides with Python&#8217;s usage of the backslash for the same purpose in string literals.<\/p>\n\n\n\n<p>For example, suppose you need to match the following string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">s = <span class=\"hljs-string\">'\\section'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In Python, the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-backslash\/\">backslash<\/a> (<code>\\<\/code>) is a special character. To construct a regular expression, you need to escape any backslashes by preceding each of them with a backslash (<code>\\<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">pattern = <span class=\"hljs-string\">'\\\\section'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In regular expressions, the pattern must be <code>'\\\\section'<\/code>. However, to express this pattern in a string literal in Python, you need to use two more backslashes to escape both backslashes again:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">pattern = <span class=\"hljs-string\">'\\\\\\\\section'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Simply put, to match a literal backslash (<code>'\\'<\/code>), you have to write <code>'\\\\\\\\'<\/code> because the regular expression must be <code>'\\\\'<\/code> and each backslash must be expressed as <code>'\\\\'<\/code> inside a string literal in Python.<\/p>\n\n\n\n<p>This results in lots of repeated backslashes. Hence, it makes the regular expressions difficult to read and understand. <\/p>\n\n\n\n<p>A solution is to use the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-raw-strings\/\">raw strings<\/a> in Python for regular expressions because raw strings treat the backslash (<code>\\<\/code>) as a literal character, not a special character.<\/p>\n\n\n\n<p>To turn a regular string into a raw string, you prefix it with the letter <code>r<\/code> or <code>R<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> re\n\ns = <span class=\"hljs-string\">'\\section'<\/span>\npattern = r<span class=\"hljs-string\">'\\\\section'<\/span>\nresult = re.findall(pattern, s)\n\nprint(result)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/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-24\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'\\\\section']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that in Python &#8216;\\section&#8217; and &#8216;\\\\section&#8217; are the same:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">p1 = <span class=\"hljs-string\">'\\\\section'<\/span>\np2 = <span class=\"hljs-string\">'\\section'<\/span>\n\n<span class=\"hljs-keyword\">print<\/span>(p1==p2) <span class=\"hljs-comment\"># true<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In practice, you&#8217;ll find the regular expressions constructed in Python using the raw strings.<\/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>A regular expression is a string that contains the special characters for matching a string with a pattern.<\/li><li>Use the <code>Pattern<\/code> object or functions in <code>re<\/code> module to search for a pattern in a string.<\/li><li>Use raw strings to construct regular expression to avoid escaping the backslashes.<\/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=\"3124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\"\n\t\t\t\tdata-post-title=\"Python Regular Expressions\"\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=\"3124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-regex\/python-regular-expressions\/\"\n\t\t\t\tdata-post-title=\"Python Regular Expressions\"\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 about Python regular expressions and how to use the most common regular expression functions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3122,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3124","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3124","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=3124"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3124\/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=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}