[Tutor] Grep equiv.
Michael P. Reilly
arcege@shore.net
Thu, 31 Aug 2000 15:31:55 -0400 (EDT)
>
> On Thu, Aug 31, 2000 at 01:20:30PM -0400, Michael P. Reilly wrote:
> > >
> > > > Is there a nice, easy, fast way of saying "if this
> > > > line/string/variable/array/whatever contains 'xxx' then do
> > > > 'something'"
> > >
> > > For literal strings use string.search()
> > >
> > > For regular expressions use re.search()
> >
> > There is also the "grep" module which gives you differing types of
> > regular expressions (does not support pcre).
> >
> > If there is no need to use regular expressions, then use the string
> > module. The overhead (even in Perl) of using a regular expression
> > engine is not always desirable.
>
> If there is no need for regular expressions and all he needs to know is
> true/false is there any reason not to use:
>
> if word in string:
> do_something()
>
The "in" works on elements in a sequence, not a slice, so the contents
of word must be a character, not a string. The string.find or
string.match functions will search for substrings.
-Arcege
--
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager | Email: arcege@shore.net |
| Salem, Mass. USA 01970 | |
------------------------------------------------------------------------