Using regexprep to clean up MATLAB code formatting
Show older comments
I was trying to put together something to fix operator spacing in a bunch of old .m files. I'm reducing this problem example to simply adding spaces around instances of = and ==. I want to ignore matches within quotes, but I realized that transposition operators on the same line mess up any sort of lookahead/lookbehind quote-counting approach I can think of.
Is there even a good way to deal with this using regex? Is there some sort of code formatting tool that I can use to accomplish this instead?
intext = sprintf(['don''t pad \n█ = █\n█ = █\n %% █=█\n''█=█''\n''█=█'' A.''\nA.'' ''█=█''\n' ...
'add pad\n█=█ A.''\nA.'' █=█\n█=█\n█==█']);
% only operate on uncommented lines
alltext = split(intext,newline);
ncom = cellfun(@isempty,(regexp(alltext,'^\s*%.*','match')));
niq = '(?=([^'']*''[^'']*'')*[^'']*$)'; % not in single quotes
alltext(ncom) = regexprep(alltext(ncom),['(?<=[^~=<>\s])=' niq],' ='); % rhs of = or ==
alltext(ncom) = regexprep(alltext(ncom),['=(?=[^=\s])' niq],'= '); % lhs
[split(intext,newline) alltext]
I'm pretty much an absolute novice with regex, and this tool is likely only going to be used once, so I'm avoiding making the regex more complicated than I can understand well enough to have confidence in it. To that end, I'm simply using masking to ignore commented lines.
7 Comments
Rik
on 31 Jan 2022
You might be able to borrow a few internal functions to do what you need. Since you have a fairly limited list, it might nog even be a bad idea to hard-code them.
Side-note: cellfun('isempty',data) is faster than a loop, which is faster than cellfun(@isempty,data).
Star Strider
on 31 Jan 2022
DGM
on 31 Jan 2022
DGM
on 31 Jan 2022
Star Strider
on 31 Jan 2022
The function itself wouldn’t. It’s the approach it takes that I’m suggesting. It first converts everything to matrix operations, then converts everything to vectorized (element-wise, array) operations. It would strip out all the spaces around the equal or equality test operators, then add the spaces back as desired.
Detecting quoted substrings first would be beyond its reach. It would simply change everything that met the criteria it was designed to detect.
DGM
on 31 Jan 2022
DGM
on 31 Jan 2022
Answers (0)
Categories
Find more on Programming 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!