简体   繁体   English

我怎样才能得到一个我不知道但知道前后单词的单词?

[英]How can I get a word that I don't know but knowing the words after and before?

I have a string like我有一个字符串

" 89 o/t rep hi my name is hi my helo, 123, A, 89 o/d rep 129 / 314 judge asdfff"

I want to get the 129 / 314 knowing that the word after it is always "judge" and the words before are "89 o/d rep" .我想让129 / 314知道它后面的词总是"judge"而前面的词是"89 o/d rep"

before = '89 o/d rep'
after = 'judge'
b = s.find(before)
a = s.find(after)
print(s[b+len(before):a])

With regex:使用正则表达式:

re.search("89 o/d rep (.*?) judge", s)[1]

Try it online! 在线尝试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得特定令牌前后的单词? - How can I get words after and before a specific token? 我如何在Python中的关键搜索词之前和之后显示2个单词 - How I display 2 words before and after a key search word in Python 我不知道该怎么做 - Can't get scrapy to follow links i don't know how to 我不希望单词准备好 word_tokenize。 我希望 word_tokenize 从 txt 文件运行字符串。 我能怎么做? - I don't want words ready to word_tokenize. I want word_tokenize to run strigs from txt file. How can I do? 我不知道如何获取Py2app - I don't know how to get Py2app 陷入无限循环,我不知道如何摆脱它 - Stuck in infinite loop and I don't know how to get out of it 我不知道如何在我的程序中添加过滤器功能来删除相同的元素,尤其是 '(', ')' 和相同的单词 - I don't know how to add a filter function in my program to remove same elements, especially '(', ')' and same words 我在python中的返回字符串后面有一个空格,我不知道如何摆脱或它来自哪里 - I have a space after a return string in python that I don't know how to get rid of or where it's coming from 如何更改此代码:我不想使用原始 SQL,但我不知道该怎么做 - How can I change this code : I don't want to use raw SQL but I don't know how can I do 我不知道如何设置自动邮件 - I don't know how to setup automail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM