parsing - Searching in an indexed string -


the plot

there rather complicatedly formatted string, there's no such readable regex parses it. , aim specific substring example, , it's original position. substring reached after parsing bit, trimming, removing beginning , searching n-th element example. want demonstrate complexity example, otherwise it's pretty general.

for demonstration, see rudimentary example. way isn't important, reach pretty complicated parse model. obviously, there can more rule , can write simplier model well.

  • firstblock{index1, index2} secondblock thirdblock
  • { firstblock {index1,index2} secondblock}
  • {firstblock secondblock thirdblock fourthblock}

i've tried make random be. parsing model like:

string text = "{ firstblock {index1,index2} secondblock}";  text = text.trim();  if (text.first() == '{') {     text = text.substring(1, text.length - 2); }  text = text.trim();  string firstblock = text.split(new char[] { ' ', '{' })[0];  text = text.remove(0, firstblock.length).trim();  string indices = "";  if (text.first() == '{') {     indices = text.split(new char[] { '{', '}' })[0];     text = text.remove(0, indices.length).trim(); }  string[] blocks = text.split(' '); 

the easy way

there way pretty easy implement , straightforward. not give correct result sometimes. way parse string , substring , make re-search, example string.indexof() , position. if there 2 match example, given first 1 though not sure wanted one.

my notion

the way think quite elegant still not consummate index characters of string @ beginning, parse it, , end proper characters , position also. problem there can't use functions library gives, , don't know way that. using snippet above:

list<tuple<int, char>> indexedtext = text                 .select((ch, index) => new tuple<int, char>(index, ch))                 .tolist(); 

and structure can still process string without library methods given position indices eventually. example, trim:

indexedtext = indexedtext                 .skipwhile(indexedchar => char.iswhitespace(indexedchar.item2))                 .tolist(); 

the actual question

the question can either new solution or way can use library methods indexed strings. aim indices after parsing string. possible there simple way out of scope haven't found proper solution yet. solution don't want simplify parsing system, said demonstration.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -