2016-03-28 72 views
-2

我想从下面的示例中选取'desired'一词。我将如何在Python中做到这一点。就像下面显示的单行注释的情况一样。我曾尝试过类似print.split(“”)[1]的东西,但它只适用于没有单行注释的情况。获取模式后的特定单词

text = /* */ 
     The desired {word} 
     /* */ 
+0

是什么让'desired'成为特殊单词? – zondo

+1

@zondo,它是*** *** ***:D(不知道,但会让提问者回应) – Bahrom

回答

0

一种选择是使用nltk,它是由ConcordanceIndex偏移来发现周围的文字:

import nltk 

text = """/* */ 
    The desired word 
    /* */""" 

tokens = nltk.word_tokenize(text) 

c = nltk.ConcordanceIndex(tokens) 
print([tokens[offset - 1] for offset in c.offsets("word")]) 

打印['desired']

+0

谢谢alecxe的帮助 –