{"id":108470,"date":"2020-02-01T14:11:45","date_gmt":"2020-02-01T14:11:45","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=5946"},"modified":"2020-02-01T14:11:45","modified_gmt":"2020-02-01T14:11:45","slug":"python-regex-how-to-match-the-start-of-line-and-end-of-line","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/02\/01\/python-regex-how-to-match-the-start-of-line-and-end-of-line\/","title":{"rendered":"Python Regex \u2013 How to Match the Start of Line (^) and End of Line ($)"},"content":{"rendered":"<p>This article is all about the <strong>start of line ^ and end of line $ regular expressions in Python&#8217;s\u00a0<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/library\/re.html\">re library<\/a>. <\/strong>These two regexes are fundamental to all regular expressions&#8212;even outside the Python world. So invest 5 minutes now and master them once and for all!<\/p>\n<h2>Python Re Start-of-String (^) Regex<\/h2>\n<p>You can use the caret operator ^ to match the beginning of the string. For example, this is useful if you want to ensure that a pattern appears at the beginning of a string. Here&#8217;s an example:<\/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=\"\">>>> import re\n>>> re.findall('^PYTHON', 'PYTHON is fun.')\n['PYTHON']<\/pre>\n<p>The findall(pattern, string) method finds all occurrences of the pattern in the string. The caret at the beginning of the pattern &#8216;^PYTHON&#8217; ensures that you match the word Python only at the beginning of the string. In the previous example, this doesn&#8217;t make any difference. But in the next example, it does:<\/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=\"\">>>> re.findall('^PYTHON', 'PYTHON! PYTHON is fun')\n['PYTHON']<\/pre>\n<p>Although there are two occurrences of the substring &#8216;PYTHON&#8217;, there&#8217;s only one matching substring&#8212;at the beginning of the string.<\/p>\n<p>But what if you want to match not only at the beginning of the string but at the beginning of each line in a multi-line string? In other words:<\/p>\n<h3>Python Re Start-of-Line (^) Regex<\/h3>\n<p>The caret operator, per default, only applies to the start of a string. So if you&#8217;ve got a multi-line string&#8212;for example, when reading a text file&#8212;it will still only match once: at the beginning of the string.<\/p>\n<p>However, you may want to match at the beginning of each line. For example, you may want to find all lines that start with &#8216;Python&#8217; in a given string.<\/p>\n<p>You can specify that the caret operator matches the beginning of each line via the re.MULTILINE flag. Here&#8217;s an example showing both usages&#8212;without and with setting the re.MULTILINE flag:<\/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=\"\">>>> import re\n>>> text = '''\nPython is great.\nPython is the fastest growing\nmajor programming language in\nthe world.\nPythonistas thrive.'''\n>>> re.findall('^Python', text)\n[]\n>>> re.findall('^Python', text, re.MULTILINE)\n['Python', 'Python', 'Python']\n>>> <\/pre>\n<p>The first output is the empty list because the string &#8216;Python&#8217; does not appear at the beginning of the string. <\/p>\n<p>The second output is the list of three matching substrings because the string &#8216;Python&#8217; appears three times at the beginning of a line.<\/p>\n<h3>Python re.sub()<\/h3>\n<p><strong>The re.sub(pattern, repl, string, count=0, flags=0)<\/strong> method returns a new string where all occurrences of the pattern in the old string are replaced by repl. Read more in <a href=\"https:\/\/blog.finxter.com\/python-regex-sub\/\">the Finxter blog tutorial<\/a>.<\/p>\n<p>You can use the caret operator to substitute wherever some pattern appears at the beginning of the string:<\/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=\"\">>>> import re\n>>> re.sub('^Python', 'Code', 'Python is \\nPython') 'Code is \\nPython'<\/pre>\n<p>Only the beginning of the string matches the regex pattern so you&#8217;ve got only one substitution.<\/p>\n<p>Again, you can use the re.MULTILINE flag to match the beginning of each line with the caret operator:<\/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=\"\">>>> re.sub('^Python', 'Code', 'Python is \\nPython', flags=re.MULTILINE) 'Code is \\nCode'<\/pre>\n<p>Now, you replace both appearances of the string &#8216;Python&#8217;.<\/p>\n<h3>Python re.match(), re.search(), re.findall(), and re.fullmatch()<\/h3>\n<p>Let&#8217;s quickly recap the most important regex methods in Python:<\/p>\n<ul>\n<li>The <strong>re.findall(pattern, string, flags=0)<\/strong> method returns a list of string matches. Read more in <a href=\"https:\/\/blog.finxter.com\/python-re-findall\/\">our blog tutorial<\/a>.<\/li>\n<li>The <strong>re.search(pattern, string<strong>, flags=0<\/strong>)<\/strong> method returns a match object of the first match. Read more in <a href=\"https:\/\/blog.finxter.com\/python-regex-search\/\">our blog tutorial<\/a>.<\/li>\n<li>The <strong>re.match(pattern, string<strong>, flags=0<\/strong>)<\/strong> method returns a match object if the regex matches at the beginning of the string. Read more in <a href=\"https:\/\/blog.finxter.com\/python-regex-match\/\">our blog tutorial<\/a>.<\/li>\n<li>The <strong>re.fullmatch(pattern, string<strong>, flags=0<\/strong>)<\/strong> method returns a match object if the regex matches the whole string. Read more in <a href=\"https:\/\/blog.finxter.com\/python-regex-fullmatch\/\">our blog tutorial<\/a>.<\/li>\n<\/ul>\n<p>You can see that all four methods search for a pattern in a given string. You can use the caret operator ^ within each pattern to match the beginning of the string. Here&#8217;s one example per method:<\/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=\"\">>>> import re\n>>> text = 'Python is Python'\n>>> re.findall('^Python', text)\n['Python']\n>>> re.search('^Python', text)\n&lt;re.Match object; span=(0, 6), match='Python'>\n>>> re.match('^Python', text)\n&lt;re.Match object; span=(0, 6), match='Python'>\n>>> re.fullmatch('^Python', text)\n>>> <\/pre>\n<p>So you can use the caret operator to match at the beginning of the string. However, you should note that it doesn&#8217;t make a lot of sense to use it for the match() and fullmatch() methods as they, by definition, start by trying to match the first character of the string.<\/p>\n<p>You can also use the re.MULTILINE flag to match the beginning of each line (rather than only the beginning of the string):<\/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=\"\">>>> text = '''Python is\nPython'''\n>>> re.findall('^Python', text, flags=re.MULTILINE)\n['Python', 'Python']\n>>> re.search('^Python', text, flags=re.MULTILINE)\n&lt;re.Match object; span=(0, 6), match='Python'>\n>>> re.match('^Python', text, flags=re.MULTILINE)\n&lt;re.Match object; span=(0, 6), match='Python'>\n>>> re.fullmatch('^Python', text, flags=re.MULTILINE)\n>>> <\/pre>\n<p>Again, it&#8217;s questionable whether this makes sense for the re.match() and re.fullmatch() methods as they only look for a match at the beginning of the string.<\/p>\n<h2>Python Re End of String ($) Regex<\/h2>\n<p>Similarly, you can use the dollar-sign operator $ to match the end of the string. Here&#8217;s an example:<\/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=\"\">>>> import re\n>>> re.findall('fun$', 'PYTHON is fun')\n['fun']<\/pre>\n<p>The findall() method finds all occurrences of the pattern in the string&#8212;although the trailing dollar-sign $ ensures that the regex matches only at the end of the string.<\/p>\n<p>This can significantly alter the meaning of your regex as you can see in the next example:<\/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=\"\">>>> re.findall('fun$', 'fun fun fun')\n['fun']<\/pre>\n<p>Although, there are three occurrences of the substring &#8216;fun&#8217;, there&#8217;s only one matching substring&#8212;at the end of the string.<\/p>\n<p>But what if you want to match not only at the end of the string but at the end of each line in a multi-line string?<\/p>\n<h3>Python Re End of Line ($)<\/h3>\n<p>The dollar-sign operator, per default, only applies to the end of a string. So if you&#8217;ve got a multi-line string&#8212;for example, when reading a text file&#8212;it will still only match once: at the end of the string.<\/p>\n<p>However, you may want to match at the end of each line. For example, you may want to find all lines that end with &#8216;.py&#8217;.<\/p>\n<p>To achieve this, you can specify that the dollar-sign operator matches the end of each line via the re.MULTILINE flag. Here&#8217;s an example showing both usages&#8212;without and with setting the re.MULTILINE flag:<\/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=\"\">>>> import re\n>>> text = '''\nCoding is fun\nPython is fun\nGames are fun\nAgreed?'''\n>>> re.findall('fun$', text)\n[]\n>>> re.findall('fun$', text, flags=re.MULTILINE)\n['fun', 'fun', 'fun']\n>>> <\/pre>\n<p>The first output is the empty list because the string &#8216;fun&#8217; does not appear at the end of the string. <\/p>\n<p>The second output is the list of three matching substrings because the string &#8216;fun&#8217; appears three times at the end of a line.<\/p>\n<h3>Python re.sub()<\/h3>\n<p><strong>The re.sub(pattern, repl, string, count=0, flags=0)<\/strong> method returns a new string where all occurrences of the pattern in the old string are replaced by repl. Read more in <a href=\"https:\/\/blog.finxter.com\/python-regex-sub\/\">the Finxter blog tutorial<\/a>.<\/p>\n<p>You can use the dollar-sign operator to substitute wherever some pattern appears at the end of the string:<\/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=\"\">>>> import re\n>>> re.sub('Python$', 'Code', 'Is Python\\nPython') 'Is Python\\nCode'<\/pre>\n<p>Only the end of the string matches the regex pattern so there&#8217;s only one substitution.<\/p>\n<p>Again, you can use the re.MULTILINE flag to match the end of each line with the dollar-sign operator:<\/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=\"\">>>> re.sub('Python$', 'Code', 'Is Python\\nPython', flags=re.MULTILINE) 'Is Code\\nCode'<\/pre>\n<p>Now, you replace both appearances of the string &#8216;Python&#8217;.<\/p>\n<h3>Python re.match(), re.search(), re.findall(), and re.fullmatch()<\/h3>\n<p>All four methods&#8212;re.findall(), re.search(), re.match(), and re.fullmatch()&#8212;search for a pattern in a given string. You can use the dollar-sign operator $ within each pattern to match the end of the string. Here&#8217;s one example per method:<\/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=\"\">>>> import re\n>>> text = 'Python is Python'\n>>> re.findall('Python$', text)\n['Python']\n>>> re.search('Python$', text)\n&lt;re.Match object; span=(10, 16), match='Python'>\n>>> re.match('Python$', text)\n>>> re.fullmatch('Python$', text)\n>>><\/pre>\n<p>So you can use the dollar-sign operator to match at the end of the string. However, you should note that it doesn&#8217;t make a lot of sense to use it for the fullmatch() methods as it, by definition, already requires that the last character of the string is part of the matching substring.<\/p>\n<p>You can also use the re.MULTILINE flag to match the end of each line (rather than only the end of the whole string):<\/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=\"\">>> text = '''Is Python\nPython'''\n>>> re.findall('Python$', text, flags=re.MULTILINE)\n['Python', 'Python']\n>>> re.search('Python$', text, flags=re.MULTILINE)\n&lt;re.Match object; span=(3, 9), match='Python'>\n>>> re.match('Python$', text, flags=re.MULTILINE)\n>>> re.fullmatch('Python$', text, flags=re.MULTILINE)\n>>><\/pre>\n<p>As the pattern doesn&#8217;t match the string prefix, both re.match() and re.fullmatch() return empty results.<\/p>\n<h2>How to Match the Caret (^) or Dollar ($) Symbols in Your Regex?<\/h2>\n<p>You know that the caret and dollar symbols have a special meaning in Python&#8217;s regular expression module: they match the beginning or end of each string\/line. But what if you search for the caret (^) or dollar ($) symbols themselves? How can you match them in a string?<\/p>\n<p>The answer is simple: escape the caret or dollar symbols in your regular expression using the backslash. In particular, use &#8216;\\^&#8217; instead of &#8216;^&#8217; and &#8216;\\$&#8217; instead of &#8216;$&#8217;. Here&#8217;s an example:<\/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=\"\">>>> import re\n>>> text = 'The product ^^^ costs $3 today.'\n>>> re.findall('\\^', text)\n['^', '^', '^']\n>>> re.findall('\\$', text)\n['$']<\/pre>\n<p>By escaping the special symbols ^ and $, you tell the regex engine to ignore their special meaning.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>You&#8217;ve learned everything you need to know about the caret operator ^ and the dollar-sign operator $ in this regex tutorial. <\/p>\n<p><strong>Summary<\/strong>: <em>The caret operator ^ matches at the beginning of a string. The dollar-sign operator $ matches at the end of a string. If you want to match at the beginning or end of each line in a multi-line string, you can set the re.MULTILINE flag in all the relevant re methods.<\/em><\/p>\n<p><strong>Want to earn money while you learn Python?<\/strong> Average Python programmers earn more than $50 per hour. You can become average, can&#8217;t you?<\/p>\n<p>Join the free webinar that shows you how to become a thriving coding business owner online!<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\">[Webinar] Are You a Six-Figure Freelance Developer?<\/a><\/p>\n<p>Join us. It&#8217;s fun! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is all about the start of line ^ and end of line $ regular expressions in Python&#8217;s\u00a0re library. These two regexes are fundamental to all regular expressions&#8212;even outside the Python world. So invest 5 minutes now and master them once and for all! Python Re Start-of-String (^) Regex You can use the caret [&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-108470","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\/108470","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=108470"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/108470\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=108470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=108470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=108470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}