Using regexprep to clean up MATLAB code formatting
12 views (last 30 days)
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
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!