MATLAB: Trying to locate tokens in text file

stringfindtokens

Hello,
I am having difficulties using regexp to find tokens and replace them. Here is the pseudocode I am working with:
(Text File Example)
This is a pretend text file. Inside of it are <TokenName> tokens that will get found and replaced with values!
text = fileread(inputFile);
tokenName = '<TokenName'>;
position = regexp(text,tokenName,'names');
It retunrs 0x0 empty cell array. I'd like to find the location of the match, and, ultimately, replace it with a value of my choosing.

Best Answer

You only use regexp names option if you have search patterns of the form (?<NAME>PATTERN) in which case it searches for the pattern and puts matches into aa structure using NAME as the field name . In your case the pattern would be <TokenName> . Note that the <> after the ? are part of the syntax whereas the <> around TokenName are literal text .
Because the content matched by the pattern is returned and your content is static there is not much point using the syntax . You would be better off with no regexp option and so returning the match position .
However .... what would probably make more sense is to go directly to regexprep to do the search and replace without caring about the position .