Here, I use tokcycle
to process the tokens of the original input and add a ,
after each character token, unless it is inside a <...>
grouping. Then, I can use listofitems
to read this comma-separated list, storing each of the items in an array \mystring
. This array is accessible by the index of the item.
If you need to access the actual tokens of each array element, the call to \mystring[...]
needs to be twice-expanded.
\documentclass{article}
\usepackage{listofitems,tokcycle}
\newcounter{nestlevel}
\Characterdirective{\ifx<#1\stepcounter{nestlevel}\else
\ifx>#1\addtocounter{nestlevel}{-1}\addcytoks{,}\else
\addcytoks{#1}\ifnum\thenestlevel=0\relax\addcytoks{,}\fi\fi\fi}
\ignoreemptyitems
\gdef\getmystring#1{%
\setcounter{nestlevel}{0}%
\tokcyclexpress{#1}%
\expandafter\readlist\expandafter\mystring\expandafter{\the\cytoks}}
\begin{document}
\getmystring{abcdef<ghi>jkl<mn>opqrs}
The number of strings is \mystringlen.
\mystring[3], \mystring[6], \mystring[7], \mystring[11], \mystring[16]
\end{document}

If one were to allow brace grouping {...}
of the input stream, rather than angle-bracket grouping <...>
, the construction would be a bit simpler, to get the same result:
\documentclass{article}
\usepackage{listofitems,tokcycle}
\stripgroupingtrue
\ignoreemptyitems
\newcommand\getmystring[1]{%
\tokcycle{\addcytoks{##1,}}{\addcytoks{##1,}}{}{}{#1}%
\expandafter\readlist\expandafter\mystring\expandafter{\the\cytoks}}
\begin{document}
\getmystring{abcdef{ghi}jkl{mn}opqrs}
The number of strings is \mystringlen.
\mystring[3], \mystring[6], \mystring[7], \mystring[11], \mystring[16]
\end{document}
SUPPLEMENT
The OP comments that he would like to use this approach where the input is provided, not directly, but by way of a \def
ed string. Here, I provide \getmydefstring
to do that:
\documentclass{article}
\usepackage{listofitems,tokcycle}
\stripgroupingtrue
\ignoreemptyitems
\newcommand\getmystring[1]{%
\tokcycle{\addcytoks{##1,}}{\addcytoks{##1,}}{}{}{#1}%
\expandafter\readlist\expandafter\mystring\expandafter{\the\cytoks}}
\newcommand\getmydefstring[1]{%
\def\tmp{\tokcycle{\addcytoks{####1,}}{\addcytoks{####1,}}{}{}}
\expandafter\tmp\expandafter{#1}%
\expandafter\readlist\expandafter\mystring\expandafter{\the\cytoks}}
\begin{document}
\def\mystring{abcdef{ghi}jkl{mn}opqrs}
\getmydefstring{\mystring}
The number of strings is \mystringlen.
\mystring[3], \mystring[6], \mystring[7], \mystring[11], \mystring[16]
\end{document}
<ghi>
or could you use the syntax{ghi}
(which would simplify things quite a bit) – David Carlisle May 6 '20 at 11:45<
or>
also? What about nesting<..>
, e.g.,ab<<cde<fg>hi>jk>lm
? – Ulrich Diez May 6 '20 at 17:49{ghi}
– Girish May 10 '20 at 17:19