mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
add missing groups() to example code
This commit is contained in:
@@ -384,9 +384,9 @@ AttributeError: 'NoneType' object has no attribute 'groups'</samp></pre>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</kbd> <span class=u>①</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern.search('work 1-(800) 555.1212 #1234').groups()</kbd> <span class=u>②</span></a>
|
||||
<samp class=pp>('800', '555', '1212', '1234')</samp>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern.search('800-555-1212')</kbd> <span class=u>③</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern.search('800-555-1212').groups()</kbd> <span class=u>③</span></a>
|
||||
<samp class=pp>('800', '555', '1212', '')</samp>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern.search('80055512121234')</kbd> <span class=u>④</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>phonePattern.search('80055512121234').groups()</kbd> <span class=u>④</span></a>
|
||||
<samp class=pp>('800', '555', '1212', '1234')</samp></pre>
|
||||
<ol>
|
||||
<li>Note the lack of <code>^</code> in this regular expression. You are not matching the beginning of the string anymore. There’s nothing that says you need to match the entire input with your regular expression. The regular expression engine will do the hard work of figuring out where the input string starts to match, and go from there.
|
||||
|
||||
Reference in New Issue
Block a user