Unable to read the last line by using regexp

1 回表示 (過去 30 日間)
Rui Zhang
Rui Zhang 2019 年 8 月 1 日
コメント済み: Walter Roberson 2019 年 8 月 2 日
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
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
Rui Zhang 2019 年 8 月 2 日
Oh, I missed a ')' in my post. You are right.
My actual code in my file is:
%% read a line
searchPattern = '(?<=\n).*?(\r)\n';
matchedText = regexp(mytext,searchPattern,'match');
Thank you very much for your help!

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2019 年 8 月 2 日
As I said, your regular expression is invalid, and even corrected doesn't work very well. I also suspect that the (\r) was meant to be (\r)?
Anyway, if the goal is just to split lines:
>> myText = sprintf('void myfunc1()\r\n{\r\n\t// body\r\n}') %demo data
myText =
'void myfunc1()
{
// body
}'
>> matchedText = regexp(myText, '\r?\n', 'split')
matchedText =
1×4 cell array
{'void myfunc1()'} {'{'} {'→// body'} {'}'}
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 2 日
Note that when you use split to split into lines that it is common to end up with an empty character vector as the last element. That shows up when the file does end in newline, because it splits at the newline and then there does not happen to be anything more in the string.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by