[Chicago] regular expression headache

Yarko Tymciurak yarkot1 at gmail.com
Mon Jan 13 19:56:37 CET 2014


On Jan 13, 2014 10:36 AM, "JS Irick" <hundredpercentjuice at gmail.com> wrote:
>
> Robert-
>
> 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)
>
> Replace this line
> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b")
> with
> regex = re.compile(r'^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b')
>

I would not offer this following "or"  - it makes things conditional.

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).


r'somethin'  keeps it clean.


> Or you could escape your slashes:
> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\\b")
>
> Thank you.  -js
>
>
> On Sat, Jan 11, 2014 at 8:42 PM, Steve Schwarz <steve at agilitynerd.com>
wrote:
>>
>> 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.
>>
>> On Saturday, January 11, 2014, Robert Spelich wrote:
>>>
>>> Steve,
>>> Thank you so much! I can't tell you how much time I spent trying to get
this to run.
>>> -Bob S.
>>>
>>>
>>> On Sat, Jan 11, 2014 at 6:21 PM, Steve Schwarz <steve at agilitynerd.com>
wrote:
>>>>
>>>> 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.
>>>> Steve
>>>>
>>>>
>>>> On Sat, Jan 11, 2014 at 5:40 PM, Robert Spelich <rlspelich at gmail.com>
wrote:
>>>>>
>>>>> import re
>>>>> address = 'PO Box 34'
>>>>> regex = re.compile("^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b")
>>>>> mo = regex.search(address)
>>>>> mo
>>>>>
>>>>> This should return a match object. It returns one when I test it at
http://www.pythonregex.com.
>>>>>
>>>>> Thanks again.-Bob
>>>>>
>>>>>
>>>>> On Sat, Jan 11, 2014 at 5:18 PM, Steve Schwarz <steve at agilitynerd.com>
wrote:
>>>>>>
>>>>>> Bob,
>>>>>> Can you post your code/tests? Then we can try it out too and help
debug what is going on.
>>>>>>
>>>>>> Best Regards,
>>>>>> Steve
>>>>>> Blogs: http://agilitynerd.com/  http://tech.agilitynerd.com/
>>>>>> Dog Agility Search: http://googility.com/
>>>>>> Dog Agility Courses: http://agilitycourses.com/
>>>>>> http://www.facebook.com/AgilityNerd
>>>>>>
>>>>>> On Sat, Jan 11, 2014 at 4:47 PM, Robert Spelich <rlspelich at gmail.com>
wrote:
>>>>>>>
>>>>>>> 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.
>>>>>>>
>>>>>>> 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.
>>>>>>>
>>>>>>> po_box = re.compile('^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b')
>>>>>>>
>>>>>>> Thanks in advance if anyone can help me out.
>>>>>>>
>>>>>>> -Bob S.
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Chicago mailing list
>>>>>>> Chicago at python.org
>>>>>>> https://mail.python.org/mailman/listinfo/chicago
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Chicago mailing list
>>>>>> Chicago at python.org
>>>>>> https://mail.python.org/mailman/listinfo/chicago
>>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Chicago mailing list
>>>>> Chicago at python.org
>>>>> https://mail.python.org/mailman/listinfo/chicago
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Best Regards,
>>>> Steve
>>>> Blogs: http://agilitynerd.com/  http://tech.agilitynerd.com/
>>>> Dog Agility Search:
>>
>>
>>
>> --
>> Sent from my phone, sorry if it is brief.
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>
>
>
> --
> ====
> JS Irick
> 312-307-8904
> Consultant: truqua.com
> Coach: atlascrossfit.com
> Programmer: juicetux.com
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20140113/b7461d18/attachment.html>


More information about the Chicago mailing list