string search function
Larry Bates
lbates at swamisoft.com
Fri Jul 23 11:20:13 EDT 2004
You should take a look at regular expressions (re)
functions. I think you will find that they are
much more efficient and can handle things your
routines can't handle.
http://www.amk.ca/python/howto/regex/
HTH,
Larry Bates
Syscon, Inc.
"gyromagnetic" <gyromagnetic at excite.com> wrote in message
news:4620daca.0407230608.55e54e95 at posting.google.com...
> Hi,
> I have written a function that searches a text string for various
> words. The text is searched using a boolean 'and' or a boolean 'or' of
> the input list of search terms.
>
> Since I need to use this function for many long strings and many
> search words, I would like to use as efficient a method as possible.
>
> Are there improvements that can be made to the code below? Are there
> better alternatives?
> I am currently using Python 2.3.
>
> Thanks.
>
> -g
>
>
> -----
>
> def bsearch(fterms, stext, btype='AND'):
> if btype == 'AND': # boolean 'and' search
> found = True
> for f in fterms:
> if f not in stext:
> found = False
> break
> else: # boolean 'or' search
> found = False
> for f in fterms:
> if f in stext:
> found = True
> break
>
> return found
>
>
>
> if __name__ == '__main__':
> stext = "hello to you all"
> terms = ['hello', 'goodbye']
> btype = 'OR'
>
> if bsearch(terms, stext, btype):
> print 'found'
> else:
> print 'not found'
More information about the Python-list
mailing list