5

I would like to find an occurrence of a word in Python and print the word after this word. The words are space separated.

example :

if there is an occurrence of the word "sample" "thisword" in a file . I want to get thisword. I want a regex as the thisword keeps on changing .

Michael
  • 78
  • 1
  • 10
Vamsi Varanasi
  • 115
  • 1
  • 1
  • 6
  • By "next to" do you mean before, after or both? Care to add an example of the sentence you will be searching in and the word you are looking to extract? – Jason Sperske Jul 16 '13 at 20:57
  • Also, can that word occur multiple times? What then? – Tim Pietzcker Jul 16 '13 at 21:06
  • By next to I mean there is another word which occurs after that word in the same line and there are multiple occurences of the word. I need a regex to get the word that occurs after the specified word. – Vamsi Varanasi Jul 16 '13 at 21:51
  • @Vamsi Are you asking for a specific solution to this problem - one that involves a regex. Or, after an answer to "How do I get the following word"... You most likely don't want a regex. – Jon Clements Jul 16 '13 at 21:58
  • @JonClements I think I do need a regex that gives the "thisword" as shown in my example as "thisword" always keeps changing – Vamsi Varanasi Jul 16 '13 at 22:02
  • @Vamsi Nope - you don't need a regex... You need to look for a string which is simpler via other methods... Find the word "sample" and output the next word... A regex is not required for this. – Jon Clements Jul 16 '13 at 22:05
  • I have been looking at lookbehind reg exp in python library. I think that suits my requirement . Could you suggest me any other method ? – Vamsi Varanasi Jul 16 '13 at 22:09

3 Answers3

32

python strings have a built in method split that splits the string into a list of words delimited by white space characters (doc), it has parameters for controlling the way it splits the word, you can then search the list for the word you want and return the next index

your_string = "This is a string"
list_of_words = your_string.split()
next_word = list_of_words[list_of_words.index(your_search_word) + 1]
crasic
  • 1,918
  • 2
  • 18
  • 29
3

Sounds like you want a function.

>>> s = "This is a sentence"
>>> sl = s.split()
>>> 
>>> def nextword(target, source):
...   for i, w in enumerate(source):
...     if w == target:
...       return source[i+1]
... 
>>> nextword('is', sl)
'a'
>>> nextword('a', sl)
'sentence'
>>> 

Of course, you'll want to do some error checking (e.g., so you don't fall off the end) and maybe a while loop so you get all the instances of the target. But this should get you started.

AnnaRaven
  • 63
  • 5
1

A very simple approach:

s = "this is a sentense"
target = "is"
words = s.split()
for i,w in enumerate(words):
    if w == target:
        # next word
        print words[i+1]
        # previous word
        if i>0:
            print words[i-1]
Vasilis
  • 2,721
  • 7
  • 33
  • 54
  • this doesn't work as the word "is" always keeps on changing. So I think it would be better If I can get a regex to do that – Vamsi Varanasi Jul 16 '13 at 21:56
  • do you want to give a few more details on how the target word is changing in order to think how the program can be rewritten? – Vasilis Jul 16 '13 at 23:42