Home > Net >  extract 2 sub strings before specific substring
extract 2 sub strings before specific substring

Time:09-29

This is my test string:

J0C,DRUMMONDVILLE,QC,CDP,K2E,NEPEAN,ON,LCD,MERIVALE,K9A,COBOURG,ON,LCD,MAIN

Whenever I see ,ON I need 2 words before that like

K2E,NEPEAN,ON,K9A,COBOURG,ON

CodePudding user response:

You can use the following regex

>>> import re
>>> s = 'J0C,DRUMMONDVILLE,QC,CDP,K2E,NEPEAN,ON,LCD,MERIVALE,K9A,COBOURG,ON,LCD,MAIN'
>>> re.findall(r'\w ,\w ,ON', s)
['K2E,NEPEAN,ON', 'K9A,COBOURG,ON']

If you want these rejoined using a comma separator you can use str.join

>>> ','.join(re.findall(r'\w ,\w ,ON', s))
'K2E,NEPEAN,ON,K9A,COBOURG,ON'

CodePudding user response:

Yet another solution (maybe more flexible):

string = "J0C,DRUMMONDVILLE,QC,CDP,K2E,NEPEAN,ON,LCD,MERIVALE,K9A,COBOURG,ON,LCD,MAIN"

class Words:
    parts = []

    def __init__(self, string):
        self.parts = string.split(",")

    def search(self, needle, wordcnt=2):
        indices = [idx for idx, item in enumerate(self.parts) if item == needle]

        for index in indices:
            if index > wordcnt:
                yield self.parts[index - wordcnt:index]


haystack = Words(string)

for part in haystack.search("ON"):
    print(part)

Which would yield

['K2E', 'NEPEAN']
['K9A', 'COBOURG']

CodePudding user response:

test_str = "J0C,DRUMMONDVILLE,QC,CDP,K2E,NEPEAN,ON,LCD,MERIVALE,K9A,COBOURG,ON,LCD,MAIN"
words = test_str.split(",")
i=0
words_you_want = []
for word in words:
    if word == "ON":
        words_you_want.append(words[i-2])
        words_you_want.append(words[i-1])
        words_you_want.append(word)
    i  = 1
print(words_you_want)

Output:

['K2E', 'NEPEAN', 'ON', 'K9A', 'COBOURG', 'ON']
  • Related