Regular expression issue

nn pruebauno at latinmail.com
Mon Aug 9 11:01:22 EDT 2010


On Aug 9, 9:18 am, genxtech <jrmy.l... at gmail.com> wrote:
> On Aug 8, 7:34 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
>
>
>
> > On 08/08/10 17:20, genxtech wrote:
>
> > > if re.search(search_string, in_string) != None:
>
> > While the other responses have addressed some of the big issues,
> > it's also good to use
>
> >    if thing_to_test is None:
>
> > or
>
> >    if thing_to_test is not None:
>
> > instead of "== None" or "!= None".
>
> > -tkc
>
> I would like to thank all of you for your responses.  I understand
> what the regular expression means, and am aware of the double negative
> nature of the test.  I guess what I am really getting at is why the
> last test returns a value of None, and even when using the syntax
> suggested in this quoted solution, the code for the last test is doing
> the opposite of the previous 2 tests that also returned a value of
> None.  I hope this makes sense and clarifies what I am trying to ask.
> Thanks


First: You understand the regular expression and the double negative
but not both of them together, otherwise you would not be asking here.
The suggestion of refactoring the code is that down the road you or
somebody else doing maintenance will have to read it again. Good books
avoid confusing grammar, likewise, good programs avoid confusing
logic.

Second: the root of your problem is the mistaken believe that P=>Q
implies (not P)=>(not Q); This is not so. Let me give an example: if
you say "if it rains" then "the ground is wet" that does not imply "if
it doesn't rain" then "the ground is not wet". You could be watering
the plants for instance. Saying "if the word finishes with a consonant
and an y" then "ay, ey, iy, oy and uy are not at the end of the word"
does not imply that "if the word does not finish with a consonant and
an y" then "ay, ey, iy, oy or uy were found at the end of the word".
The word could end in x for instance.

I hope I didn't make it more confusing, otherwise other people will
probably chime in to make it clear to you.



More information about the Python-list mailing list