Extract substrings between any pair of delimiters | Function to print strings present between any pair of delimeters ; Stores the indices ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of substring ; Extract the substring ; Driver Code
def printSubsInDelimeters ( string ) : NEW_LINE INDENT dels = [ ] ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == ' [ ' ) : NEW_LINE INDENT dels . append ( i ) ; NEW_LINE DEDENT elif ( string [ i ] == ' ] ' and len ( dels ) != 0 ) : NEW_LINE INDENT pos = dels [ - 1 ] ; NEW_LINE dels . pop ( ) ; NEW_LINE length = i - 1 - pos ; NEW_LINE ans = string [ pos + 1 : pos + 1 + length ] ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " [ This ▁ is ▁ first ] ▁ ignored ▁ text ▁ [ This ▁ is ▁ second ] " ; NEW_LINE printSubsInDelimeters ( string ) ; NEW_LINE DEDENT