简体   繁体   English

在字符串 python 中找到句子时如何提取下一个单词

[英]How to extract next word when a sentence is found in a string python

I am trying to find an efficient way with which I can look for a sentence in a string if found then extract the next word after that for example -我试图找到一种有效的方法,如果找到,我可以在字符串中查找一个句子,然后提取下一个单词,例如 -

string_text = """"
 create table AWSBilling202004(identity_LineItemId VARCHAR(512), identity_TimeInterval VARCHAR(512),
 create table AWSBilling202004_tagMapping (remappedUserTag VARCHAR(512), userTag VARCHAR(512));
 insert into AWSBilling202004_tagMapping(remappedUserTag, userTag) values('userTag4', 'user:BillingTeam'
 create table AWSBilling202004_costCategoryMapping (remappedCostCategory VARCHAR(512), costCategory VARCHAR(512));
 """"

In the above text whenever create table is found I would like to extract the next word after that.在上面的文本中,每当找到create table时,我想在此之后提取下一个单词。 Output for the above code should be上面代码的 Output 应该是

AWSBilling202004
AWSBilling202004_tagMapping
AWSBilling202004_costCategoryMapping

As you see above when a ( is found word is being extracted till that point.正如你在上面看到的,当一个(被提取到那个点时。

I have been looking at regex solutions but finding it difficult to get them to work for my use case.我一直在研究正则表达式解决方案,但发现很难让它们适用于我的用例。 I would really appreciate any guidance or help.我非常感谢任何指导或帮助。

This worked for me这对我有用

import re
def get_next_words(text, pattern):
    return re.findall("%s\s+([a-zA-Z0-9_]+)"%(pattern), text)


string_text = '''
 create table AWSBilling202004(identity_LineItemId VARCHAR(512), identity_TimeInterval VARCHAR(512),
 create table AWSBilling202004_tagMapping (remappedUserTag VARCHAR(512), userTag VARCHAR(512));
 insert into AWSBilling202004_tagMapping(remappedUserTag, userTag) values('userTag4', 'user:BillingTeam'
 create table AWSBilling202004_costCategoryMapping (remappedCostCategory VARCHAR(512), costCategory VARCHAR(512));
'''


print(get_next_words(string_text, "create table"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM