What is the difference between Python's re.search and re.match?

PythonServer Side ProgrammingProgramming

<p style="">Both re.match() and re.search() are methods of the Python module re.</p><p>The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string &lsquo;TP Tutorials Point TP&rsquo; and looking for a pattern &lsquo;TP&rsquo; will match.&nbsp;</p><h2>example</h2><pre class="prettyprint notranslate">import re result = re.match(r&#39;TP&#39;, &#39;TP Tutorials Point TP&#39;) print result.group(0)</pre><h2>Output</h2><pre class="result notranslate">TP</pre><p style="">The re.search() method is similar to re.match() but it doesn&rsquo;t limit us to find matches at the beginning of the string only.&nbsp;</p><h2 style="">example</h2><pre class="prettyprint notranslate">import re result = re.search(r&#39;Tutorials&#39;, &#39;TP Tutorials Point TP&#39;) print result.group(0)</pre><h2 style="">Output</h2><pre class="result notranslate">Tutorials</pre><p>Here you can see that, search() method is able to find a pattern from any position of the string.</p>
Updated on 18-Feb-2020 07:23:33