Trying to locate tokens in text file
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cyrus
el 20 de Nov. de 2018
Respondida: Walter Roberson
el 20 de Nov. de 2018
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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 20 de Nov. de 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 .
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!