replacing all occurrences of a string in a file using regexp

Hello,
I have a very large file that contains sections like these below
VariableProp('GND_R', 'UD', '', '130mm')
VariableProp('T', 'UD', '', '1mm')
VariableProp('RC', 'UD', '', '38mm')
.
.
.
VariableProp('ALFAD', 'UD', '', '255deg')
I need to find all the occurrences of VariableProp(VarName, 'UD', '', VarValue) in the file, and change the VarValue
I am still learning the regexp topic, so in this case help would be appreciated
Thank you

4 Comments

The regexprep function will allow you to replace text with regular expressions. Did you already write a regular expression that matches what you want?
I've found https://regexr.com/ to be a useful resource for figuring out regular expressions (though there may be some minor disagreement with regexp(), not sure), but Jan's prototype is more Matlabby, easier to understand, and perhaps more performant.
You are not giving many details.
What are the requirements for those names? Can any characters occurs between those quotes? If that includes single quotes, how are those escaped?
You say you want to change the VarValue part, but you haven't given any indication of what kind of change you want to make.

Sign in to comment.

 Accepted Answer

Is it required to use regexp? If not:
C = readlines(FileName);
m = startsWith(C, "VariableProp('" + VarName + ", 'UD', '', ");
C(m) = sprintf("VariableProp('%s', 'UD', '', '%g%s')", VarName, newValue, newUnit);
writelines(FileName, C);

6 Comments

Thanks, but VarName is not know a priori
Okay. In my suggested code VarName is a variable also. So please explain it again: You have this text file:
VariableProp('GND_R', 'UD', '', '130mm')
VariableProp('T', 'UD', '', '1mm')
VariableProp('RC', 'UD', '', '38mm')
What do you want the code to do based on which inputs?
Thank you for bearing with me.
The text file contains a set of variable names, and variables values. both unknown a priori.
The only thing I know isthat the file includes the string "VariableProp(", which I suppose would be the searching criterion.
I need the code
  1. to find the variables names (GND_R, T,RC and all others) and the corresponding variable values (130mm,1mm,38mm, etc.)
  2. to change the variable values to new values
The original text file includes (among many other things) these sets:
VariableProp('GND_R', 'UD', '', '130mm')
VariableProp('T', 'UD', '', '1mm')
VariableProp('RC', 'UD', '', '38mm')
The changed text file would include these sets
VariableProp('GND_R', 'UD', '', '80mm')
VariableProp('T', 'UD', '', '2mm')
VariableProp('RC', 'UD', '', '10mm')
hope that clarifies my question
Thank you
You need to explain the type of characters that can form these names. You're asking for help, why are you not answering our questions for clarification?
And what pattern is there in the variables that need to change? Can you explain the rules?
@Robert Jones: I guess you have these input data beside the file:
newData = {'GND_R', '130mm'; ...
'T', '1mm'; ...
'RC', '38mm'};
If you post the format of your input, a better matching suggestion could be given.
Then you can expand my code:
C = readlines(FileName);
for k = 1:height(newData)
VarName = newData{k, 1};
Value = newData{k, 2};
m = startsWith(C, "VariableProp('" + VarName + ", 'UD', '', ");
C(m) = sprintf("VariableProp('%s', 'UD', '', '%s')", VarName, Value);
end
writelines(FileName, C);

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Asked:

on 10 Nov 2022

Commented:

on 14 Nov 2022

Community Treasure Hunt

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

Start Hunting!