{"id":116593,"date":"2020-08-12T08:28:58","date_gmt":"2020-08-12T08:28:58","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11749"},"modified":"2020-08-12T08:28:58","modified_gmt":"2020-08-12T08:28:58","slug":"python-one-line-regex-match","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/08\/12\/python-one-line-regex-match\/","title":{"rendered":"Python One Line Regex Match"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>Summary<\/strong>: To match a <code>pattern<\/code> in a given <code>text<\/code> using only a single line of Python code, use the one-liner <code>import re; print(re.findall(pattern, text))<\/code> that imports the regular expression library re and prints the result of the <code>findall()<\/code> function to the shell.<\/p>\n<p><strong>Problem<\/strong>: Given a string and a regular expression pattern. Match the string for the regex pattern&#8212;in a single line of Python code!<\/p>\n<p><strong>Example<\/strong>: Consider the following example that matches the pattern <code>'F.*r'<\/code> against the string <code>'Learn Python with Finxter'<\/code>.<\/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\ns = 'Learn Python with Finxter'\np = 'F.*r'\n# Found Match of p in s: 'Finxter'<\/pre>\n<p>Let&#8217;s dive into the different ways of writing this into a single line of Python code!<\/p>\n<p> <iframe loading=\"lazy\" height=\"600px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/BriskAcidicWatch?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code. What&#8217;s the output of each method? Why does the output differ?<\/em><\/p>\n<p><strong><em>Do you want to master the regex superpower?<\/em><\/strong> Check out my new book <em><strong><a href=\"https:\/\/blog.finxter.com\/ebook-the-smartest-way-to-learn-python-regex\/\" title=\"[eBook] The Smartest Way to Learn Python Regex\">The Smartest Way to Learn Regular Expressions in Python<\/a><\/strong><\/em> with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. <\/p>\n<h2>Method 1: findall()<\/h2>\n<p>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>.<\/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=\"\"># Method 1: findall()\nimport re; print(re.findall('F.*r', 'Learn Python with Finxter'))\n# ['Finxter']<\/pre>\n<p>There&#8217;s no better way of importing the <code><a href=\"https:\/\/blog.finxter.com\/python-regex\/\" title=\"Python Regex Superpower [Full Tutorial]\">re<\/a><\/code> library and calling the <code>re.findall()<\/code> function in a single line of code&#8212;you must use the semicolon <code>A;B<\/code> to separate the statements <code>A<\/code> and <code>B<\/code>. <\/p>\n<p>The <code>findall()<\/code> function finds all occurrences of the pattern in the string. <\/p>\n<h2>Method 2: search()<\/h2>\n<p>The <strong>re.search(pattern, string, flags=0)<\/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>.<\/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=\"\"># Method 2: search()\nimport re; print(re.search('F.*r', 'Learn Python with Finxter'))\n# &lt;re.Match object; span=(18, 25), match='Finxter'><\/pre>\n<p>The <code>search()<\/code> function finds the first match of the pattern in the string and returns a matching object<\/p>\n<h2>Method 3: match()<\/h2>\n<p>The <strong>re.match(pattern, string, flags=0)<\/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>.<\/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=\"\"># Method 3: match()\nimport re; print(re.match('.*F.*r', 'Learn Python with Finxter'))\n# &lt;re.Match object; span=(0, 25), match='Learn Python with Finxter'><\/pre>\n<p>The <code>match()<\/code> function finds the match of the pattern <em>at the beginning<\/em> of the string and returns a matching object. In this case, the whole string matches, so the match object encloses the whole string.<\/p>\n<h2>Method 4: fullmatch()<\/h2>\n<p>The <strong>re.fullmatch(pattern, string, flags=0)<\/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>.<\/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=\"\"># Method 4: fullmatch()\nimport re; print(re.fullmatch('.*F.*r.*', 'Learn Python with Finxter'))\n#&lt;re.Match object; span=(0, 25), match='Learn Python with Finxter'><\/pre>\n<p>The <code>fullmatch()<\/code> function attempts to match the whole string and returns a matching object if successful. In this case, the whole string matches, so the match object encloses the whole string.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: To match a pattern in a given text using only a single line of Python code, use the one-liner import re; print(re.findall(pattern, text)) that imports the regular expression library re and prints the result of the findall() function to the shell. Problem: Given a string and a regular expression pattern. Match the string for [&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-116593","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\/116593","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=116593"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116593\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=116593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=116593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=116593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}