0

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. Output for the above code should be

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.

Ajay Misra
  • 191
  • 3
  • 13

1 Answers1

2

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"))
Abhijith Asokan
  • 1,865
  • 10
  • 14