For example, given a string "A, B, C (P, Q, R), D (X, Y [K, L, M ,N],
Z)".
Would like to split into tokens thusly:
a[0] == "A"
a[1] == "B"
a[2] == "C (P, Q, R)"
a[3] == "D (X, Y [K, L, M ,N], Z)"
i.e. do not descend into sub-lists
PHP split() using commas as a delimiter will give 14 tokens.
I can write a routine which checks the input byte by byte and
increments or decrements a counter based on how many opening "( [ {"
or closing ") ] }" brackets it sees. If counter 0, this means ignore
delimiters (i.e. keep looking). Guaranteed to work, but to my mind
seems to be rather clunky.
Is it possible to extract the tokens using regular expressions? E.g.
substitute highest level commas with a special delimiter say "~", and
split using that delimiter.
Thanks for reading.
Regards,
YR
Z)".
Would like to split into tokens thusly:
a[0] == "A"
a[1] == "B"
a[2] == "C (P, Q, R)"
a[3] == "D (X, Y [K, L, M ,N], Z)"
i.e. do not descend into sub-lists
PHP split() using commas as a delimiter will give 14 tokens.
I can write a routine which checks the input byte by byte and
increments or decrements a counter based on how many opening "( [ {"
or closing ") ] }" brackets it sees. If counter 0, this means ignore
delimiters (i.e. keep looking). Guaranteed to work, but to my mind
seems to be rather clunky.
Is it possible to extract the tokens using regular expressions? E.g.
substitute highest level commas with a special delimiter say "~", and
split using that delimiter.
Thanks for reading.
Regards,
YR
Comment