Re: string search function

From: Larry Bates (lbates_at_swamisoft.com)
Date: 07/23/04


Date: Fri, 23 Jul 2004 10:20:13 -0500

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@excite.com> wrote in message
news:4620daca.0407230608.55e54e95@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'



Relevant Pages

  • Re: Getting the file name from a FILE *
    ... string -- which might perhaps include the filename openned with, ... but as there can be considerable differences in parameter ... passing between non-varargs routines and varargs routines, ... I expect that that recompile / relink step would not be considered ...
    (comp.lang.c)
  • Re: Serious Perl Regular Expression deficiency?
    ... I started doing Perl 2 years ago and have ... > conclusion that regular expressions have a serious ... This is serious because the not string ... If you want to pull out the contents of XML comments you could do this. ...
    (comp.lang.perl.misc)
  • Re: Remove characters from string
    ... and your link took me to the templates page at microsoft office. ... there expaining regular expressions unless you meant I should search for it. ... | them to the same format for ease of processing. ... | the string I remove extraneous characters. ...
    (microsoft.public.excel.programming)
  • Re: How do you get the tail end of a string?
    ... that often string manipulation is done via regular expressions anyway ... Ruby has anonymous closures. ... regular expressions for string manipulations that don't specifically require ... defined width (say a log file). ...
    (comp.lang.ruby)
  • Re: Trying to execute something stored in variable
    ... Public Function ExecuteMethods(ByVal prmMethodName As String, ... Dim objMethodInfo As MethodInfo ... Public Sub SubRoutine1() ... Please note that I cannot take full credit for these routines. ...
    (microsoft.public.dotnet.languages.vb)