Trying to locate tokens in text file

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Nov 2018
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 .

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!