[Tutor] Advanced String Search using operators AND, OR etc..

Alan Gauld alan.gauld at btinternet.com
Mon May 4 20:03:46 CEST 2009


"Alex Feddor" <alex.feddor at gmail.com> wrote

> I am looking for method enables advanced text string search. Method
> string.find() or re module seems no  supporting what I am looking for. 
> The
> idea is as follows:

The re module almost certainly can do what you want but regex
are notoriously hard to master and often obscure.

> Text ="FDA meeting was successful. New drug is approved for whole sale
> distribution!"
>
> Example 01:
> search criteria:  "FDA" AND ( "approve*" OR "supported")

The regex will search for FDA followed by either approve or supported.
There is no AND operator in regex since AND just implies a sequence
within the string. There is an OR operator however which is '|'

> The catch is that in Text variable FDA and approve words  are not one 
> after
> another (other words are in between).

And regex allows for you to specify a sequence of anything after FDA

> Example 02:
> search criteria: "Ben"
> The catch is that code sould find only exact Ben words not also words 
> which
> that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben 
> is
> the right word we are looking for.

And again regex provides ways of ensuring an exact match.

> I would really appreciated your advice - code sample / links how above 
> can
> be achieved! if possible I would appreciated solution achieved with free 
> of
> charge module.

You need to go through one of the many regex tutorials to understand
what can be done with these extremely powerful search tools (and
what can't!) There is a very basic introduction in my tutorial which
unfortunately doesn't cover all that you need here but might be a
good starting point.

The python HOWTO is another  good start and goes a bit deeper
with a different approach:

http://docs.python.org/howto/regex.html

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list