<div dir="ltr"><p dir="ltr"><br>
On Jan 13, 2014 10:36 AM, "JS Irick" <<a href="mailto:hundredpercentjuice@gmail.com" target="_blank">hundredpercentjuice@gmail.com</a>> wrote:<br>
><br>
> Robert-<br>
><br>
> You need to use raw strings for regular expressions, otherwise your regexp special characters will not be properly interpreted.  (experts, please correct any terms I have wrong here)<br>
><br>
> Replace this line<br>
> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b")<br>
> with<br>
> regex = re.compile(r'^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b')<br>
></p>
<p dir="ltr">I would not offer this following "or"  - it makes things conditional.</p>
<p dir="ltr">The issue is that strings get interpreted on read, so if you put the string in a variable, how many times does it get interpreted?  What about when you put it in a variable, and append or otherwise change it (someday) programmatically (think:  settings).</p>
<p dir="ltr"><br></p><p dir="ltr">r'somethin'  keeps it clean.</p><p dir="ltr"><br>
> Or you could escape your slashes:<br>
> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\\b")<br>
><br>
> Thank you.  -js<br>
><br>
><br>
> On Sat, Jan 11, 2014 at 8:42 PM, Steve Schwarz <<a href="mailto:steve@agilitynerd.com" target="_blank">steve@agilitynerd.com</a>> wrote:<br>
>><br>
>> For me, when it comes to writing regexps I always write a ton of test strings ordered easy to hard and start from very short simple regexps until they all pass. <br>
>><br>
>> On Saturday, January 11, 2014, Robert Spelich wrote:<br>
>>><br>
>>> Steve,<br>
>>> Thank you so much! I can't tell you how much time I spent trying to get this to run.<br>
>>> -Bob S.<br>
>>><br>
>>><br>
>>> On Sat, Jan 11, 2014 at 6:21 PM, Steve Schwarz <<a href="mailto:steve@agilitynerd.com" target="_blank">steve@agilitynerd.com</a>> wrote:<br>
>>>><br>
>>>> I tested it in python 2.5, 2.7.6 and 3.3 and it fails in all of them. Seems to be the "\b", when I remove it it matches.<br>
>>>> Steve<br>
>>>><br>
>>>><br>
>>>> On Sat, Jan 11, 2014 at 5:40 PM, Robert Spelich <<a href="mailto:rlspelich@gmail.com" target="_blank">rlspelich@gmail.com</a>> wrote:<br>
>>>>><br>
>>>>> import re<br>
>>>>> address = 'PO Box 34'<br>
>>>>> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b")<br>
>>>>> mo = regex.search(address)<br>
>>>>> mo<br>
>>>>><br>
>>>>> This should return a match object. It returns one when I test it at <a href="http://www.pythonregex.com" target="_blank">http://www.pythonregex.com</a>.<br>
>>>>><br>
>>>>> Thanks again.-Bob<br>
>>>>><br>
>>>>><br>
>>>>> On Sat, Jan 11, 2014 at 5:18 PM, Steve Schwarz <<a href="mailto:steve@agilitynerd.com" target="_blank">steve@agilitynerd.com</a>> wrote:<br>
>>>>>><br>
>>>>>> Bob,<br>
>>>>>> Can you post your code/tests? Then we can try it out too and help debug what is going on.<br>
>>>>>><br>
>>>>>> Best Regards,<br>
>>>>>> Steve<br>
>>>>>> Blogs: <a href="http://agilitynerd.com/" target="_blank">http://agilitynerd.com/</a>  <a href="http://tech.agilitynerd.com/" target="_blank">http://tech.agilitynerd.com/</a><br>
>>>>>> Dog Agility Search: <a href="http://googility.com/" target="_blank">http://googility.com/</a><br>
>>>>>> Dog Agility Courses: <a href="http://agilitycourses.com/" target="_blank">http://agilitycourses.com/</a><br>
>>>>>> <a href="http://www.facebook.com/AgilityNerd" target="_blank">http://www.facebook.com/AgilityNerd</a><br>
>>>>>><br>
>>>>>> On Sat, Jan 11, 2014 at 4:47 PM, Robert Spelich <<a href="mailto:rlspelich@gmail.com" target="_blank">rlspelich@gmail.com</a>> wrote:<br>
>>>>>>><br>
>>>>>>> Can anyone offer me a suggestion as to why a regular expression that validates on a testing tool would not match on the same string when run in a script or directly in the interpreter? I used 2 different online tools, one a general expression tool and one Python specific. They both matched, when run locally it does not match.<br>


>>>>>>><br>
>>>>>>> I am using an re from a cookbook to match addresses in a database that start with a PO Box address. I am using 2.7. I am stumped.<br>
>>>>>>><br>
>>>>>>> po_box = re.compile('^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b')<br>
>>>>>>><br>
>>>>>>> Thanks in advance if anyone can help me out.<br>
>>>>>>><br>
>>>>>>> -Bob S.<br>
>>>>>>><br>
>>>>>>> _______________________________________________<br>
>>>>>>> Chicago mailing list<br>
>>>>>>> <a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
>>>>>>> <a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
>>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> -- <br>
>>>>>><br>
>>>>>><br>
>>>>>> _______________________________________________<br>
>>>>>> Chicago mailing list<br>
>>>>>> <a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
>>>>>> <a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
>>>>>><br>
>>>>><br>
>>>>><br>
>>>>> _______________________________________________<br>
>>>>> Chicago mailing list<br>
>>>>> <a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
>>>>> <a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
>>>>><br>
>>>><br>
>>>><br>
>>>><br>
>>>> -- <br>
>>>> Best Regards,<br>
>>>> Steve<br>
>>>> Blogs: <a href="http://agilitynerd.com/" target="_blank">http://agilitynerd.com/</a>  <a href="http://tech.agilitynerd.com/" target="_blank">http://tech.agilitynerd.com/</a><br>
>>>> Dog Agility Search:<br>
>><br>
>><br>
>><br>
>> -- <br>
>> Sent from my phone, sorry if it is brief.<br>
>><br>
>> _______________________________________________<br>
>> Chicago mailing list<br>
>> <a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
>><br>
><br>
><br>
><br>
> -- <br>
> ====<br>
> JS Irick<br>
> <a href="tel:312-307-8904" value="+13123078904" target="_blank">312-307-8904</a><br>
> Consultant: <a href="http://truqua.com" target="_blank">truqua.com</a><br>
> Coach: <a href="http://atlascrossfit.com" target="_blank">atlascrossfit.com</a><br>
> Programmer: <a href="http://juicetux.com" target="_blank">juicetux.com</a><br>
><br>
> _______________________________________________<br>
> Chicago mailing list<br>
> <a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
><br>
</p>
</div>