######################################################################## ## ## Copyright (C) 2022-2031 The Octave Project Developers ## See for copyright notices. ## ## This file is part of Octave. ## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## ######################################################################## ## -*- texinfo -*- ## @deftypefn {} {} extractAfter (var{text}, @var{seperator}) ## @deftypefnx {} {@var{extractedText} =} extractAfter (@dots{}) ## ## Cuts @var{text} from the end of a @var{seperator} up to the end of the @var{text} itself ## ## INPUT ## @var{text} is a string, a character array, or a cell array of strings / character arrays ## @var{seperator} is a single character or a character array. But it can also be ## a number N. A cell array containing numbers or charaters is also possible. ## ## OUTPUT ## @var{extractedText} is @var{text}(end_position_of_the_first_seperator + 1:end) or ## in case of numbers @var{text} is cut as @var{text}(N + 1:end). If @var{text} and @var{seperator} are ## cell arrays they are processed pairwise. ## @seealso{extractBefore} ## @end deftypefn function extractedText = extractAfter(text,seperator) ## test the number of input parameters. They have to be exactly 2. if (nargin != 2) print_usage(); endif ## free from cell if length is only 1 if (iscell (text) && length (text) == 1) text = text{1}; endif if (iscell (seperator) && length (seperator) == 1) seperator = seperator{1}; endif ## convert parameters if (isstring (text)) text = char (text); ## for a octave string implementation endif ## test first parameter if (iscell (text)) valid = logical (sum (!cellfun ('ischar', text)) == 0); ## all cell entries have to be char arrays else valid = ischar (text); ## text has to be char endif if (!valid) error ('extractAfter: first argument has to be char or a cell array of chars. For additional information see help extractAfter.'); endif ## test second parameter ## all cell entries have to be char arrays or numbers if (iscell (seperator)) valid = (sum (!cellfun (@(x) ischar (x) || isscalar (x), seperator, 'UniformOutput', true)) == 0); else valid = ischar (seperator) || isscalar (seperator); endif if (!valid) error ('extractAfter: second argument has to be cell, char or number. For additional information see help extractAfter.'); endif ## mutual changes if (iscell (seperator) && ischar (text)) text = repmat ({text}, size (seperator)); ## multiply text if seperator is a cell elseif (iscell (text) && (ischar (seperator) || isscalar (seperator))) seperator = repmat ({seperator}, size (text)); ## multiply seperator if text is a cell endif if (iscell (seperator) && iscell (text) && length (seperator) != length (text)) error ('extractAfter: cell length of text and seperator has to match'); endif ## cellify to simplify programm flow if (ischar (seperator) || isscalar (seperator)) seperator = {seperator}; endif if (ischar (text)) text = {text}; endif ## do the cuts extractedText = text; if (iscell (extractedText) && iscell (seperator)) n = length (extractedText); for i = 1:n if (ischar (seperator{i})) seperator (i) = min (strfind (extractedText{i}, seperator{i})) + length (seperator{i}) - 1; endif if (isempty (seperator (i))) seperator (i) = 0; endif if (seperator{i} <= length (extractedText{i})) extractedText(i) = extractedText{i}(seperator{i} + 1:end); else error (sprintf ('extractAfter: seperator exceeds the text length in position ##i.', i)); endif endfor endif ## free from cell if length is only 1 if (iscell (extractedText) && length (extractedText) == 1) extractedText = extractedText{1}; endif end