Lineanchor not working in regexp
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Rahul
2024년 10월 7일
Hi @Omkar, can you provide the text document 'output.txt' mentioned in the post, that'll help in better understanding the issue.
채택된 답변
Rahul
2024년 10월 9일
편집: Rahul
2024년 10월 9일
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
추가 답변 (1개)
Stephen23
2024년 10월 7일
편집: Stephen23
2024년 10월 7일
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
참고 항목
카테고리
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!