Simple regular expression

Joel Corbin zaikenv at gmail.com
Fri Jun 27 13:21:18 EDT 2008


If you read John's message carefully (which is the output of
"help(re.search)") you can see the difference between "re.search" and
"re.match". The former looks for a regex anywhere in the given string, the
latter requires the string to begin with the given regex.

Joel

On Fri, Jun 27, 2008 at 12:26 PM, python_enthu <srhegde at gmail.com> wrote:

> On Jun 27, 11:05 am, "John Salerno" <johnj... at NOSPAMgmail.com> wrote:
> > "python_enthu" <srhe... at gmail.com> wrote in message
> >
> > news:47031cc4-2b5e-43b3-9338-cf4a7b4c1568 at d45g2000hsc.googlegroups.com.
> ..
> >
> > >I am trying this.. what is wrong in this..
> >
> > > IDLE 1.2.2
> > >>>> import re
> > >>>> a="my name is fname lname"
> > >>>> p=re.compile('name')
> > >>>> m=p.match (a)
> > >>>> print p.match(a)
> > > None
> >
> >       match( string[, pos[, endpos]])
> >
> > If zero or more characters at the beginning of string match this regular
> > expression, return a corresponding MatchObject instance. Return None if
> the
> > string does not match the pattern; note that this is different from a
> > zero-length match.
> >
> >       search( string[, pos[, endpos]])
> >
> > Scan through string looking for a location where this regular expression
> > produces a match, and return a corresponding MatchObject instance. Return
> > None if no position in the string matches the pattern; note that this is
> > different from finding a zero-length match at some point in the string.
>
> Thanks John Salerno and John Machin,
>
> I am used to perl. So I guess I am better of using re.search instead
> of re.match
> BTW, is re.search('string') equivalent to re.match ('^string')
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080627/81ea5a63/attachment-0001.html>


More information about the Python-list mailing list