I have a raw text in a column named "message" as shown below:
Example dataframe
Date message
2020-11-01 ['some not required text1', 'Startstring ,
'some not required text2', MandatorySubstring , 'some not
required text3', 'ID :AB_CD, 'Stopstring' 'some not
required text4', 'Startstring ID :cd_ab', 'some not
required text5', 'ID :ghed','some not required text6', ID :zyx', 'Stopstring 'some
not required text7']
2020-11-02 ['some not required text8', Startstring 'ID :ABCD',
'some not required text9', 'ID :ED_GH', 'some not
required text10', ID :X_YZ, Stopstring 'some not
required text11', 'Startstring
'some not required text12',MandatorySubstring
'some not required text13', ID :z_yx', 'some not required text14''Stopstring
'some not required text15']
I am looking to extract the string just after ID : in between Startstring and Stopstring if MandatorySubstring exists between Startstring and Stopstring and discarding the IDs if MandatorySubstring doesn't exists between Startstring and Stopstring. There may be multiple such instances in one Date.
Expected Output:
Date message
2020-11-01 AB_CD
2020-11-02 z_yx
I tried following pattern:
pattern = StartString\s*((?:(?!StartString).)*?MandatoryString 1.*?)\s*Stopstring
It gives me all text between the startstring and stoptstring. I have no clue how to extract text just after ID: from this text now.
Can Anyone provide me Regex pattern for this scenario? Help in this regard is highly appreciated. Thanks