Writing Grep matches into a global string variable


Curious if there is a way to write several strings into one global string variable. For example, I have a 1D text wave with chemical formulas. I need to pull formulas that contain [15N] and store them all together in a global string, each formula separated by a ";". I was trying to do something like this:

// labelindex is the wave containing index numbers of cells containing [15N] in the chemical formulas.
// labelindex is created using Grep.
variable count = dimsize(labelindex,0)
for(i=0;i<count;i+=1)  //run through the text wave
    string name
    sprintf name,"%s", textwave[labelindex[i]]  //textwave contains all the chemical formulas
    print name
    name = replacestring("[",name,"/[")  //to avoid regex acting up on "[" or "]"
    name = replacestring("]",name,"/]")
    SNRcolornames = name   //SNRcolornames is the global string


I know this is not accurate yet because the end result of this is SNRcolornames with only one string recorded in it, which is the last match in the textwave. 

How do I make SNRcolornames store all matches separated by a ";"? 


Thanks for the help! 



Is AddListItem your friend here?


See also the other related functions to deal with string lists.


function/S matchlist(wave/T textwave, string regex)
    string strList = ""
    Extract/free textwave, wmatches, GrepString(textwave, regex)
    wfprintf strList, "%s;", wmatches
    return strList


Thanks, Tony! I was able to work with GrepString for a different problem I was trying to tackle.