re bug, or am I missing something? 
Author Message
 re bug, or am I missing something?

It appears to me that the re module is broken, or at least it's not
working in a way that I'm expecting it to.  I'm trying to match any
strings that end in a digit character.  Works fine in perl, doesn't work
in Python.  Is this actually broken, or am I missing something?

===============================================

if ("foo5"=~/[0-9]$/) { print "matched\n" }
else { print "I'm confused now\n" }
^D
matched

===============================================

Quote:
>>> import re
>>> if re.match("[0-9]$","foo5"):

...     print "matched"
... else:
...     print "I'm confused now."
...
I'm confused now.


Sat, 05 May 2001 03:00:00 GMT  
 re bug, or am I missing something?
What you're missing is that "match" is not "search".

Quote:

> It appears to me that the re module is broken, or at least it's not
> working in a way that I'm expecting it to.  I'm trying to match any
> strings that end in a digit character.  Works fine in perl, doesn't
> work in Python.  Is this actually broken, or am I missing something?

> ===============================================

> if ("foo5"=~/[0-9]$/) { print "matched\n" }
> else { print "I'm confused now\n" }
> ^D
> matched

> ===============================================
> >>> import re
> >>> if re.match("[0-9]$","foo5"):
> ...     print "matched"
> ... else:
> ...     print "I'm confused now."
> ...
> I'm confused now.

- Gordon


Sat, 05 May 2001 03:00:00 GMT  
 re bug, or am I missing something?
Hi All--

Quote:

> It appears to me that the re module is broken, or at least it's not
> working in a way that I'm expecting it to.  I'm trying to match any
> strings that end in a digit character.  Works fine in perl, doesn't work
> in Python.  Is this actually broken, or am I missing something?

> ===============================================

> if ("foo5"=~/[0-9]$/) { print "matched\n" }
> else { print "I'm confused now\n" }
> ^D
> matched

> ===============================================
> >>> import re
> >>> if re.match("[0-9]$","foo5"):
> ...     print "matched"
> ... else:
> ...     print "I'm confused now."
> ...
> I'm confused now.

I believe this is a FAQ issue.  Change ``re.match'' to ``re.search'' and
it'll work.

Didn't Uncle Timmy have some wise sayings regarding the placement of the
documentation for these two functions?

Take care,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.

http://www.pauahtun.org
----------------------------------------------



Sat, 05 May 2001 03:00:00 GMT  
 re bug, or am I missing something?

Quote:
> > >>> import re
> > >>> if re.match("[0-9]$","foo5"):
> > ...     print "matched"
> > ... else:
> > ...     print "I'm confused now."
> > ...
> > I'm confused now.
> I believe this is a FAQ issue.  Change ``re.match'' to ``re.search'' and
> it'll work.

Ah, OK.  Much better.

I'm very new to Python, so perhaps this will become clearer with time, but
why is it necessary to have two "use this regular expression on this
string" methods?  It seems to me that you could use
re.search("^(pattern)") and get re.match()-like behavior.

OTOH, criticizing a language I've just recently started learning
seems about as couth as showing up to a party and immediately questioning
the choice of music or hors d'oeuvres.  I shall be quiet now.



Sat, 05 May 2001 03:00:00 GMT  
 re bug, or am I missing something?
Hi All--

[bobbit]

Quote:
> Ah, OK.  Much better.

> I'm very new to Python, so perhaps this will become clearer with time, but
> why is it necessary to have two "use this regular expression on this
> string" methods?  It seems to me that you could use
> re.search("^(pattern)") and get re.match()-like behavior.

re.match is somewhat faster than re.search, because it knows to bail out
instantly if the first character of what it's looking for doesn't match
the first character of what it's looking in.

An analogy is the C/C++ idiom
        char * s; // Search string
        char * p; // Search pattern
        if(s[0]==p[0])
        {
                if(strcmp(s,p))
                        ...
which is known to substantially speed up applications doing lots of
strcmp() operations.

Quote:
> OTOH, criticizing a language I've just recently started learning
> seems about as couth as showing up to a party and immediately questioning
> the choice of music or hors d'oeuvres.  I shall be quiet now.

Oh, it never stopped any of us;-)

<both-feet-in-the-mud-puddle>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.

http://www.pauahtun.org
----------------------------------------------



Sat, 05 May 2001 03:00:00 GMT  
 re bug, or am I missing something?
[Soren Ragsdale]

Quote:
> ...
> I'm very new to Python, so perhaps this will become clearer with time,
> but why is it necessary to have two "use this regular expression on
> this string" methods?  It seems to me that you could use
>    re.search("^(pattern)")
> and get re.match()-like behavior.

There is no general way to spell what "match" can do using "search".  When
you start crunching huge strings in pieces with the optional "starting
position" argument, you'll quickly find that a leading "^" doesn't mean
"match starting here" -- "^" still means "start of the buffer or immediately
following a newline", which is not at all what you'll need.  Even for a
single search, "^" may not mean "start of buffer", depending on single-line
vs multi-line mode.  match does exactly what you need with no hassles in
such cases.

Quote:
> OTOH, criticizing a language I've just recently started learning
> seems about as couth as showing up to a party and immediately
> questioning the choice of music or hors d'oeuvres.  I shall be
> quiet now.

Too late!  We already booted you out the door, and won't let you back in
until you have something effusively nice to say.  Note that sincerity is not
required, however -- your next post just has to suck up a *little* <wink>.

python's-house-mother-ly y'rs  - tim



Sun, 06 May 2001 03:00:00 GMT  
 re bug, or am I missing something?

Quote:

> I'm very new to Python, so perhaps this will become clearer with time, but
> why is it necessary to have two "use this regular expression on this
> string" methods?  It seems to me that you could use
> re.search("^(pattern)") and get re.match()-like behavior.

It's not the same! There is a flag (re.MULTILINE) you can give to
the search/match to make ^ match a beginning of a line and match
$ the end of a line.

If you now want to match "(pattern)$" where the $ means
end of line, you would have to give the re.MULTILINE flag
as parameter:
  re.match("(pattern)$",text,re.MULTILINE)
will match
  text="pattern"
and
  text="pattern\nfoo" # $ matches before the \n
but not
  text="foo\npattern"

*BUT*
  re.search("^(pattern)$",re.MULTILINE)
will also match
  text="foo\npattern"
because the re.MULTILINE will apply to the ^ as well!

QED. So, there *is* a difference between search and match ;-)

Michael

--
     ''''\     Michael Scharf

    `    >     http://www.TakeFive.com



Sun, 06 May 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Is this a bug or am I missing something?

2. Is this a reference count bug or am I missing something?

3. Am i missing something with CW5Pe?

4. Am I missing something here?

5. Am I missing something here?

6. Clarion 4 Scroll Bars - Am I Missing Something

7. Am I missing something in forth!

8. Haskell: am I missing something?

9. GNAT.Regexp: Am I missing something?

10. Am I missing something?

11. itk::Toplevel and -menu - am I missing something

12. am i missing something ?

 

 
Powered by phpBB® Forum Software