[Tutor] Grep equiv.

alan.gauld@bt.com alan.gauld@bt.com
Thu, 31 Aug 2000 17:33:11 +0100


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

eg:

import re
if re.search('[pP]ython','My python script'):
   print 'found it!'


There are also substitution functions for search/replace operations...
Read the re module docs.

Alan G.


> 
> With something being spit it out, return true, or whatever.  Doesn't
> matter.
> 
> Anyhow, thanks for any help :)
> 
> Wes
> A struggling python beginner, trying to buck the system in a 
> building full
> of c, perl, awk, java, and other assorted programmers - none 
> of whom use
> python (yet ;) )
> 
> 
>