Trying to locate tokens in text file

2 ビュー (過去 30 日間)
Cyrus
Cyrus 2018 年 11 月 20 日
回答済み: Walter Roberson 2018 年 11 月 20 日
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.

採用された回答

Walter Roberson
Walter Roberson 2018 年 11 月 20 日
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 .

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by