Help to solve a regexp problem

4 ビュー (過去 30 日間)
Kouichi C. Nakamura
Kouichi C. Nakamura 2016 年 7 月 8 日
編集済み: Stephen23 2016 年 7 月 8 日
I'm trying to capture comment block in a string (char array). Regular expression '%.*(\n|$)' captures string from % to the end of the line.
regexp(sprintf(' %%this is a comment'),'%.*(\n|$)','match')
ans =
cell
'% this is a comment'
However, what I want to do is to capture multiple incidence of comment blocks in a char array. The above expression only captures the first one, failing to match '% and this'.
regexp(sprintf(' %%this is a comment\n %%and this\n'),'%.*(\n|$)','match')
ans =
cell
'% this is a comment…'
Could somebody help me about this?
  2 件のコメント
Stephen23
Stephen23 2016 年 7 月 8 日
編集済み: Stephen23 2016 年 7 月 8 日
To make writing regular expressions easier, you might like to try using my FEX submission that lets you interactively write and develop regular expressions:
Simply run it to open the interactive figure, then enter your data string, and start playing around with the regular expression. The figure will update and show you the regexp outputs as you change the regular expression. This makes it fast and easy to try different regular expressions, and to check how changes affect the outputs.
PS: This page is very useful too. Lots of information, but worth reading and referring to:
Kouichi C. Nakamura
Kouichi C. Nakamura 2016 年 7 月 8 日
This looks really interesting. I'll surely give it a try. I could help me a lot. Thanks!

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

採用された回答

per isakson
per isakson 2016 年 7 月 8 日
Test this
>> str = sprintf(' %%this is a comment\n %%and this\n');
>> cac = regexp( str, '[ ]*%[^\n]+', 'match' )
cac =
' % this is a comment' ' % and this'
>> whos cac
Name Size Bytes Class Attributes
cac 1x2 294 cell
  2 件のコメント
Kouichi C. Nakamura
Kouichi C. Nakamura 2016 年 7 月 8 日
Thanks a lot. Yours included leading spaces (maybe I was not clear enough about what I want to catch), but '%[^\n]+' successfully did the job!
>> cac = regexp(str, '%[^\n]+', 'match' )
cac =
1×2 cell array
'% this is a comment' '% and this'
Guillaume
Guillaume 2016 年 7 月 8 日
Note that your regular expression would have worked if you'd used the non-greedy *?:
regexp(sprintf(' %%this is a comment\n %%and this\n'),'%.*?(\n|$)','match')
Nonetheless, per's expression is probably more efficient.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by