regexprep delete parenthesis in a sentence. not working
1 次查看(过去 30 天)
显示 更早的评论
im trying to remove the bolded area. because of the () regexprep doesnt seem to find the sentance to remove. is there a work around for this so i can get rid of it all including the ()?
text file
{
Name = PTC_MATERIAL_SUB_TYPE
Type = String
Default = 'LINEAR'
Access = Locked
},
{
Name = PTC_THERMAL_CONDUCTIVITY
Type = Real
Default = NaN W/(m K)
Access = Full
},
{
Name = PTC_MASS_DENSITY
Type = Real
Default = 1.000000 lbm/in^3
Access = Full
fid = fopen('trial.txt', 'rt') % Open source file.
g= fscanf(fid,'%c')
expression7 = '\n{\n Name = PTC_THERMAL_CONDUCTIVITY\n Type = Real\n Default = NaN W/(m K)\n Access = Full\n},\n{';
replace7 = '';
newStr7 = regexprep(g,expression7,replace7);
回答(1 个)
dpb
2021-12-2
编辑:dpb
2021-12-2
It's short enough, I'll repost the working code from previous Answer again...
txt=textread('bogar.txt','%s','whitespace','','delimiter',newline); % read file a cellstr array
idxHdr=find(contains(txt,'{')); % find the section headers
idxTrlr=find(contains(txt,'},')); % and the closing trailers
idxNaN=find(contains(txt,'NaN')); % then where the offending NaNs are
ixDel=flip(interp1(idxHdr,1:numel(idxHdr),idxNaN,'previous')); % find sections containing NaN
for i=ixDel(:).' % iterate over sections
txt(idxHdr(i):idxTrlr(i))=[]; % remove between header/trailer
end
NB: This version includes the refinement in the Comment earlier to search for the opening "{" instead of "Name" since the sections are not nested.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!