Unable to read the last line by using regexp
이전 댓글 표시
I used the following code to read text line by line. But the result excludes the last line. For example, if my text is:
void myfunc1()
{
// body;
}
by using the following code:
matchedText = regexp(myText,'(?<=\n.*?(\r)\n','match');
The matchedText is:
matchedText = {'void myfunc1()','{','','//body',''};
How can I get the last line of the text? -- '}' is missing.
Thanks.
댓글 수: 7
Guillaume
2019년 8월 1일
I doubt your regular expression matches anything since it has unbalanced brackets. If the whole purpose is just to split text into lines, then it's overly complex anyway.
What are you trying to do?
dpb
2019년 8월 1일
Or in memory, what about splitlines()?
Walter Roberson
2019년 8월 2일
The last line might possibly not have a \r\n -- the file might just end.
Rui Zhang
2019년 8월 2일
Guillaume
2019년 8월 2일
Again, the regexp that you've written in the question is invalid.
>> myText = sprintf('\nvoid myfunc1()\r\n{\r\n\t// body\r\n}')
myText =
'
void myfunc1()
{
// body
}'
>> matchedText = regexp(myText,'(?<=\n.*?(\r)\n','match') %exact copy/paste of what you wrote in the question
matchedText =
0×0 empty cell array
The regexp is missing a closing bracket. Maybe you meant (notice the ) after the first \n)
>> matchedText = regexp(myText,'(?<=\n).*?(\r)\n','match')
matchedText =
1×3 cell array
{'void myfunc1()←↵'} {'{←↵'} {'→// body←↵'}
You'll notice that:
- I had to start the text with a \n because of the look-behind
- you capture all the line return\line feed
Again, there are much simpler ways to split text into lines.
Rui Zhang
2019년 8월 2일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!