RE Module Question.
sholden at holdenweb.com
Tue Nov 6 13:18:06 CET 2001
"Adonis Vargas" <deltapigz at telocity.com> wrote in ...
> i am trying to fidn specific word within a string and am unable to
> findanything suitable for it, i have tried looking into the RE module but
> just is written in hebrew, and can not grasp any of it. looked into the
> python.org/howto's and under the library/module index and nothing i just
> cant understand it. is there a simple example to this or simple
> all i want to do is find i.e.:
> test = "this is a test"
> if "test" in test: print "found string."
> this is just pseudocode to get the concept im trying to achieve.
> any help would greatly be appreciated.
For your problem the phrase "what's in a word?" is very relevant. One of the
hardest things about any kind of lexical analysis is determining exactlky
what comprises a word ("lexical token", more formally).
However, from your example, it seems like you might be satisfied with
everything between spaces in your input. For this purpose the easiest
approach is probably to use the split() method to split the string into a
list of "words", and then see whetehr your required target appears in the
>>> test = "this is a test"
>>> wl = test.split()
['this', 'is', 'a', 'test']
>>> if "is" in wl:
... print "Found 'is'"
Other posters have also made suggestions about using index(), but you would
have to be careful there about ensuring that you found a word, and not just
a part of a word. Now you can go on to start thinking about upper- and
lower-case differences, apostrophes and all the other good stuff that makes
string processing fun!
More information about the Python-list