mercredi 28 octobre 2015

Extract locations of a string and associate information with this parts

I have a string that inside has some kind of metadata that "describe" parts of the string.
Example:

This is an {TypeAStart}arbitrary long{TypeAEnd} text which has {TypeBStart}various{TypeBEnd} usages  

What I want is to get the indexes of the strings that are contained inside these tags.
My first thought was something like:

String[] tags = [ "{TypeA", "{TypeB", etc ];  
for(String tag:tags)  {  
   int start = mainString.indexOf(tag + "Start");  
   if(start != -1) {  
      int end = mainString.indexOf(tag + "End}", start);  
      // store somewhere start,end  
   } 
}   

But I think this approach is hacky and error prone. How can I do this efficiently with regexes?

UPDATE:
I have this string as I mentioned and these metadata are marks where styling is to be applied. So my main problem is to find an effective way to extract the positions of the string that the styling is to be applied (marked by these custom tags) and be able to figure out for each tag (which maps to a style) which indexes/pars of the string are affected. My main target is to have the original string stripped of the tags and in another data structure the indexes and styles.

Aucun commentaire:

Enregistrer un commentaire