Lineanchor not working in regexp
2 views (last 30 days)
Show older comments
Hi,
I'm trying to read the content of my text file in matlab. I am using fileread to get text, but if I match lineanchor expression to get ending characters of line I don't get correct output.
str = fileread('output.txt');
expr = '.$';
lastInLine = regexp(str,expr,'match','lineanchors')
Can anyone help? thanks.
2 Comments
Rahul
on 7 Oct 2024
Hi @Omkar, can you provide the text document 'output.txt' mentioned in the post, that'll help in better understanding the issue.
Accepted Answer
Rahul
on 9 Oct 2024
Edited: Rahul
on 9 Oct 2024
Hi Omkar,
Assuming you're trying to find ending characters of each line, inside a text document, using the "regexp" function in MATLAB R2017a. Based on the text document provided, '\r\n' are present as line ending characters instead of standard Unix-style '\n' newline characters, which can be verified using the following script:
% Read the file content as a single string
fileID = fopen('input.txt', 'r');
text = fscanf(fileID, '%c');
fclose(fileID);
% Check if '\r\n' exists in the text
if contains(text, sprintf('\r\n'))
disp('The file contains Windows-style line endings (\r\n).');
elseif contains(text, sprintf('\n'))
disp('The file contains Unix-style line endings (\n).');
else
disp('No standard line endings found.');
end
The 'lineanchor’ option for 'regexp' uses Unix-style line ending '\n', as its implicit assumption. It may be helpful to note that the 'fileread' function does not convert '\r\n' into plain '\n', so if your text document uses '\r\n', then the ‘lineanchor’ option may not interpret this as the end of a line.
This specific behavior has been highlighted in documentations of ‘regexp’ in MATLAB R2018b and later releases:
A possible workaround could be to replace all '\r\n' to '\n' using 'strrep' or 'regexprep' functions, before finding line ending characters using 'regexp':
% Replace Windows newlines with Unix newlines
text_new = strrep(str, sprintf('\r\n'), newline);
% Perform lineanchor regular expression matching
expression = '.$';
lastInLine = regexp(text_new,expression,'match','lineanchors')
To know more about usage of ‘regexp’, ’regexprep’ and ‘strrep’ functions, refer to the documentation links mentioned below:
- https://www.mathworks.com/help/releases/R2019b/matlab/ref/regexp.html?searchHighlight=regexp&s_tid=doc_srchtitle
- https://www.mathworks.com/help/matlab/ref/strrep.html
- https://www.mathworks.com/help/matlab/ref/regexprep.html
Hope that helped!
0 Comments
More Answers (1)
Stephen23
on 7 Oct 2024
Edited: Stephen23
on 7 Oct 2024
tx1 = sprintf('Hello\r\nWorld'); % Windows
tx2 = sprintf('Hello\nWorld'); % *nix, MacOS
ma1 = regexp(tx1,'.$','match','lineanchors');
ma1{:}
ma2 = regexp(tx2,'.$','match','lineanchors');
ma2{:}
One solution is to replace the Windows newline characters with a simple linefeed:
tx3 = regexprep(tx1,'\r\n','\n');
ma3 = regexp(tx3,'.$','match','lineanchors');
ma3{:}
0 Comments
See Also
Categories
Find more on Characters and Strings 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!