Syntax
nam=strfindtag(txt,srch,[endstr,endnl])
Input Parameter
txt : | | text |
srch : | | search string |
endstr : | | end tag to |
endnl : | | optional new line |
Output Parameter
Examples
[~,b]=system('sw_vers')
strfindtag(b,'ProductName:',newline)
Copyright 2021-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This function, strfindtag
, is designed to extract a substring from a given text based on a search string and optional end conditions. It is part of the SolidGeometry library.
Input Parameters
- txt: The main text from which a substring is to be extracted.
- srch: The search string that marks the beginning of the desired substring.
- endstr: (Optional) A string that marks the end of the desired substring. If not provided, the function uses an empty string as default.
- endnl: (Optional) A boolean indicating whether a new line should be considered as an end condition. Defaults to
false
.
Output Results
- nam: The extracted and trimmed substring from the text.
- nxt: The remaining text after the extracted substring, also trimmed.
Algorithm Steps
- Retrieve the
endstr
and endnl
parameters using the getfuncparams
function. If not provided, default values are used.
- Use the
strfindafter
function to find the start and end indices (ss
and ee
) of the substring within txt
that starts after srch
and ends before endstr
or a new line if endnl
is true.
- Extract the substring from
txt
using the indices ss
and ee
, and trim any leading or trailing whitespace to get nam
.
- Extract the remaining text after the end of the substring, starting from
ee + 1 + numel(endstr)
to the end of txt
, and trim it to get nxt
.
Example Usage
The function can be used to extract a product name from system information:
[~,b]=system('sw_vers')
strfindtag(b,'ProductName:',newline)
This example searches for the string 'ProductName:' in the output of the sw_vers
command and extracts the product name, considering a new line as the end condition.
Algorithm explaination created using ChatGPT on 2025-08-18 23:28. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21