Function is = findtags(s,t1,t2)
is = findtags(s,t1,t2,allow_nesting,at_templ)
[is,err] = findtags(...)
[is,err,iserr] = findtags(...)
returns a matrix is, containing information about
single and paired tags found in the input string s.
The tags are given by input arguments t1 and t2,
which are two one-dimensional cell arrays of same
lengths. For a single tag i, t1{i} is a non-empty
string, and t2{i} is an empty string. For a paired
tag i, both t1{i} and t2{i} are non-empty strings.
Each row in output matrix is relates to a single tag
or to a pair of tags found in s. Matrix is has 7
columns:
-- column 1: index in s of the begin of t1{i};
-- column 2: index in s of the end of t1{i};
-- column 3: index in s of the begin of t2{i};
-- column 4: index in s of the en of t2{i};
-- column 5: index in t1 and t2, thus i;
-- column 6: nesting level;
-- column 7: one for the single tag, zero for
the pair of tags.
The rows in matrix is are always sorted, i.e. in
the ordre of the appearance of single and opening
tags in string s.
Matrix is will be empty, if no tags have been
found, or if an error occurred.
To get the text between the k'th found pair of tags,
write:
s( (is(k,2)+1):(is(k,3)-1) )
In this way, the result will be automatically an
empty string if there happen to be no text between
the tags.
Among other cases, error messages are given when
paired tags do not seem to occur orderly in string s.
If optional input argument allow_nesting is false,
an error message is given when tags are
nested, e.g. when "<a><a></a></a>" or
"<a><b></b></a>" or "<a><c></a>" occur for given
t1={"<a>", "<b>", "<c>"} and t2={"</a>","</b>", ""}.
At default, allow_nesting is true.
Independently to allow_nesting, an error message
is given when paired tags are incorrectly nested,
e.g. in "<a><b></a></b>" (correct nestings would be
"<a><b></b></a>" or "<b><a></a></b>").
Optional input argument at_templ is a string,
which has the value "index %d" at default. It
is used in error messages to indicate at which
position in string s an error occurs. Therefore,
at_templ contains a placeholder for integers,
"%d". It is possible to recalculate the position
into e.g. a line and column index of a text; in
that case, at_templ may be "line %d column %d".
Optional output argument err contains error
messages; it is a cell array. For each single
error message m, err{m,1} contains a string with
the text (possibly including the contents of
at_templ) and err{m,2} contains a one-dimensional
array for error positions (if any), i.e. an array
of zero or more indices in string s.
Optional output argument iserr is a one-
dimensional array of indices, formed by
concatenation of err(:,2).
If the output argument err is not requested, error
messages will be printed on screen rightaway.
This example code in relation to err:
[is,err] = findtagpairs(s,t1,t2,[],"character %d");
if (size(err,1) > 0),
iserr = cat(2,err(:,2){:});
error(["-- " strjoin(err(:,1),"\n-- ") "\n"],iserr);
endif;
is equivalent to:
[is,err,iserr] = findtagpairs(s,t1,t2,[],"character %d");
if (size(err,1) > 0),
error(["-- " strjoin(err(:,1),"\n-- ") "\n"],iserr);
endif;
An example when error positions are recalculated:
[is,err,iserr] = findtagpairs(s,t1,t2,[],"line %d column %d");
if (size(err,1) > 0),
ir_ic = [];
for p = iserr,
ir = ... calculate line index based on p ...;
ic = ... calculate column index based on p ...;
ir_ic = [ir_ic ir ic];
endfor;
iserr = cat(2,err(:,2){:});
error(["-- " strjoin(err(:,1),"\n-- ") "\n"],ir_ic);
endif;
(c) 2026 fabien van mook
2026.08.20 release of this file within package "fvm-miscellaneous" under GNU GPLv3+