Search code examples
pythonrspotfire

How to Extract the words containing a specific combination of letters from a long string in spotfire?


For example from the below string,

abc6:ContextData abc6:xyz1 iCare abc6:xyz2 abc6:xyz3  abc6:xyz4 <abc6:xyz5  abc6:xyz6  abc6:xyz7 abc6:ContextData

I want to extract the words preceded by "abc6." For "abc6:xyz3" I want the suffix xyz3. For the longer example, the output would be like:

ContextData,xyz1,xyz2,xyz3,xyz4,xyz5,xyz6,xyz7,ContextData

do we need any regular expression for these?


Solution

  • An R-base Solution based on substr function is:

    z  <- "abc6:ContextData abc6:xyz1 iCare abc6:xyz2 abc6:xyz3 abc6:xyz4 <abc6:xyz5 abc6:xyz6 abc6:xyz7 abc6:ContextData"
    z1 <- unlist(strsplit(z, split=" "))
    z2 <- z1[substr(z1, start=1, stop=5)=="abc6:"]
    z3 <- substr(z2, start=6, stop=nchar(z2))
    cat(z3, sep=",")
    

    Result:

    ContextData,xyz1,xyz2,xyz3,xyz4,xyz6,xyz7,ContextData