Sick Gaming
[Tut] Python Regex Search - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python Regex Search (/thread-93299.html)



[Tut] Python Regex Search - xSicKxBot - 01-24-2020

Python Regex Search

<div><p>When I first learned about regular expressions, I didn’t appreciate their power. But there’s a reason regular expressions have survived seven decades of technological disruption: coders who understand regular expressions have a massive advantage when working with textual data. They can write in a single line of code what takes others dozens!</p>
<p>This article is all about the search() method of Python’s&nbsp;<a rel="noreferrer noopener" target="_blank" href="https://docs.python.org/3/library/re.html">re library</a>. To learn about the easy-to-use but less powerful findall() method that returns a list of string matches, check out our article about the similar&nbsp;<a rel="noreferrer noopener" target="_blank" href="https://blog.finxter.com/python-re-findall/">findall() method</a>.</p>
<p>So how does the re.search() method work? Let’s study the specification.</p>
<h2>How Does re.search() Work in Python?</h2>
<p><strong>The re.search(pattern, string) method matches the first occurrence of the pattern in the string and returns a match object. </strong></p>
<p><strong>Specification</strong>:</p>
<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.search(pattern, string, flags=0)</pre>
<p>The re.search() method has up to three arguments.</p>
<ul>
<li><strong>pattern</strong>: the regular expression pattern that you want to match.</li>
<li><strong>string</strong>: the string which you want to search for the pattern.</li>
<li><strong>flags </strong>(optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know <a href="https://blog.finxter.com/python-regex-flags/">how to use those flags? Check out this detailed article</a> on the Finxter blog.</li>
</ul>
<p>We’ll explore them in more detail later. </p>
<p><strong>Return Value:</strong></p>
<p>The re.search() method returns a match object. You may ask (and rightly so):</p>
<h2>What’s a Match Object?</h2>
<p>If a regular expression matches a part of your string, there’s a lot of useful information that comes with it: what’s the exact position of the match? Which regex groups were matched—and where? </p>
<p>The <a href="https://docs.python.org/3/library/re.html#match-objects">match object</a> is a simple wrapper for this information. Some regex methods of the re package in Python—such as search()—automatically create a match object upon the first pattern match.</p>
<p>At this point, you don’t need to explore the match object in detail. Just know that we can access the start and end positions of the match in the string by calling the methods m.start() and m.end() on the match object m:</p>
<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="">>>> m = re.search('h...o', 'hello world')
>>> m.start()
0
>>> m.end()
5
>>> 'hello world'[m.start():m.end()] 'hello'</pre>
<p>In the first line, you create a match object m by using the re.search() method. The pattern ‘h…o’ matches in the string ‘hello world’ at start position 0. You use the start and end position to access the substring that matches the pattern (using the popular <a href="https://blog.finxter.com/introduction-to-slicing-in-python/">Python technique of slicing</a>).</p>
<p>Now, you know the purpose of the match() object in Python. Let’s check out a few examples of re.search()!</p>
<h2>A Guided Example for re.search()</h2>
<p>First, you import the re module and create the text string to be searched for the regex patterns:</p>
<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
>>> text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. '''</pre>
<p>Let’s say you want to search the text for the string ‘her’:</p>
<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.search('her', text)
&lt;re.Match object; span=(20, 23), match='her'></pre>
<p>The first argument is the pattern to be found. In our case, it’s the string ‘her’. The second argument is the text to be analyzed. You stored the multi-line string in the variable text—so you take this as the second argument. You don’t need to define the optional third argument <em>flags</em> of the search() method because you’re fine with the default behavior in this case.</p>
<p>Look at the output: it’s a match object! The match object gives the span of the match—that is the start and stop indices of the match. We can also directly access those boundaries by using the start() and stop() methods of the match object:</p>
<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="">>>> m = re.search('her', text)
>>> m.start()
20
>>> m.end()
23</pre>
<p>The problem is that the search() method only retrieves the first occurrence of the pattern in the string. If you want to find all matches in the string, you may want to use the<a href="https://blog.finxter.com/python-re-findall/"> findall() method</a> of the re library.</p>
<h2>What’s the Difference Between re.search() and re.findall()?</h2>
<p>There are two differences between the re.search(pattern, string) and re.findall(pattern, string) methods:</p>
<ul>
<li>re.search(pattern, string) returns a match object while re.findall(pattern, string) returns a list of matching strings.</li>
<li>re.search(pattern, string) returns only the first match in the string while re.findall(pattern, string) returns all matches in the string.</li>
</ul>
<p>Both can be seen in the following example:</p>
<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 superior to Python'
>>> re.search('Py...n', text)
&lt;re.Match object; span=(0, 6), match='Python'>
>>> re.findall('Py...n', text)
['Python', 'Python']</pre>
<p>The string ‘Python is superior to Python’ contains two occurrences of ‘Python’. The search() method only returns a match object of the first occurrence. The findall() method returns a list of all occurrences.</p>
<h2>What’s the Difference Between re.search() and re.match()?</h2>
<p>The methods re.search(pattern, string) and re.match(pattern, string) both return a match object of the first match. However, re.match() attempts to match at the beginning of the string while re.search() matches anywhere in the string.</p>
<p>You can see this difference in the following code:</p>
<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 = 'Slim Shady is my name'
>>> re.search('Shady', text)
&lt;re.Match object; span=(5, 10), match='Shady'>
>>> re.match('Shady', text)
>>></pre>
<p>The re.search() method retrieves the match of the ‘Shady’ substring as a match object. But if you use the re.match() method, there is no match and no return value because the substring ‘Shady’ does not occur at the beginning of the string ‘Slim Shady is my name’. </p>
<h2>How to Use the Optional Flag Argument?</h2>
<p>As you’ve seen in the specification, the search() method comes with an optional third ‘flag’ argument:</p>
<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.search(pattern, string, flags=0)</pre>
<p>What’s the purpose of the flags argument?</p>
<p>Flags allow you to control the regular expression engine. Because regular expressions are so powerful, they are a useful way of switching on and off certain features (for example, whether to ignore capitalization when matching your regex). </p>
<figure class="wp-block-table is-style-stripes">
<table class="">
<tbody>
<tr>
<td><strong>Syntax</strong></td>
<td><strong>Meaning</strong></td>
</tr>
<tr>
<td> <strong>re.ASCII</strong></td>
<td>If you don’t use this flag, the special Python regex symbols \w, \W, \b, \B, \d, \D, \s and \S will match Unicode characters. If you use this flag, those special symbols will match only ASCII characters — as the name suggests. </td>
</tr>
<tr>
<td> <strong>re.A</strong> </td>
<td>Same as re.ASCII </td>
</tr>
<tr>
<td> <strong>re.DEBUG</strong> </td>
<td>If you use this flag, Python will print some useful information to the shell that helps you debugging your regex. </td>
</tr>
<tr>
<td> <strong>re.IGNORECASE</strong> </td>
<td>If you use this flag, the regex engine will perform case-insensitive matching. So if you’re searching for [A-Z], it will also match [a-z]. </td>
</tr>
<tr>
<td> <strong>re.I</strong> </td>
<td>Same as re.IGNORECASE </td>
</tr>
<tr>
<td> <strong>re.LOCALE</strong> </td>
<td>Don’t use this flag — ever. It’s depreciated—the idea was to perform case-insensitive matching depending on your current locale. But it isn’t reliable. </td>
</tr>
<tr>
<td> <strong>re.L</strong> </td>
<td>Same as re.LOCALE </td>
</tr>
<tr>
<td> <strong>re.MULTILINE</strong> </td>
<td>This flag switches on the following feature: the start-of-the-string regex ‘^’ matches at the beginning of each line (rather than only at the beginning of the string). The same holds for the end-of-the-string regex ‘$’ that now matches also at the end of each line in a multi-line string. </td>
</tr>
<tr>
<td> <strong>re.M</strong> </td>
<td>Same as re.MULTILINE </td>
</tr>
<tr>
<td> <strong>re.DOTALL</strong> </td>
<td>Without using this flag, the dot regex ‘.’ matches all characters except the newline character ‘\n’. Switch on this flag to really match all characters including the newline character. </td>
</tr>
<tr>
<td> <strong>re.S</strong> </td>
<td>Same as re.DOTALL </td>
</tr>
<tr>
<td> <strong>re.VERBOSE</strong> </td>
<td>To improve the readability of complicated regular expressions, you may want to allow comments and (multi-line) formatting of the regex itself. This is possible with this flag: all whitespace characters and lines that start with the character ‘#’ are ignored in the regex. </td>
</tr>
<tr>
<td> <strong>re.X</strong> </td>
<td>Same as re.VERBOSE </td>
</tr>
</tbody>
</table>
</figure>
<p>Here’s how you’d use it in a practical example:</p>
<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 great!'
>>> re.search('PYTHON', text, flags=re.IGNORECASE)
&lt;re.Match object; span=(0, 6), match='Python'></pre>
<p>Although your regex ‘PYTHON’ is all-caps, we ignore the capitalization by using the flag re.IGNORECASE.</p>
<h2>Where to Go From Here?</h2>
<p>This article has introduced the re.search(pattern, string) method that attempts to match the first occurrence of the regex pattern in a given string—and returns a match object.</p>
<p>Python soars in popularity. There are two types of people: those who understand coding and those who don’t. The latter will have larger and larger difficulties participating in the era of massive adoption and penetration of digital content. Do you want to increase your Python skills daily without investing a lot of time? </p>
<p><a href="https://blog.finxter.com/subscribe/">Then join my “Coffee Break Python” email list of tens of thousands of ambitious coders!</a></p>
</div>


https://www.sickgaming.net/blog/2020/01/09/python-regex-search/