How do I find the last occurrence of a match using regexp in MATLAB?

54 ビュー (過去 30 日間)
Swati Tiwari
Swati Tiwari 2013 年 8 月 15 日
If I have the following string in MATLAB:
str = '/* This is a comment */ int x; /* sectionEndExample */';
How do I find the last comment that contains sectionEndExample? I have tried the following:
expr = ['^.*/\*.*sectionEndExample.*\*/'];
sectionEndIdx1 = regexp(str, expr);
But this always returns the sectionEndIdx1 as 1. I am looking in the documentation and have so far played around with the lookAround options. However, I can't figure out a way to do it in MATLAB :(
  3 件のコメント
Swati Tiwari
Swati Tiwari 2013 年 8 月 16 日
I apologize for not framing the question correctly. I want to be able to find the last comment in the line. I believe I could get this by tweaking the regular expression. But, my efforts have been in vain.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 16 日
Ok, but what about the answers below?

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 16 日
str = '/* This is a comment */ int x; /* sectionEndExample */';
pattern='(/)\*[\w\s]+\*(/)';
[sectionEndIdx1,debut,fin] = regexp(str, pattern,'match','start','end');
sectionEndIdx1=sectionEndIdx1{end}
start_comment=debut(end)
end_comment=fin(end)

その他の回答 (3 件)

Cedric
Cedric 2013 年 8 月 15 日
編集済み: Cedric 2013 年 8 月 15 日
Is it what you want? If so, we can work a bit to improve it (in particular for allowing stars in the comment if relevant, which are not allowed with this pattern).
>> regexp(str, '[^\*]+(?=\*/$)', 'match')
ans =
' sectionEndExample '

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 15 日
str = '/* This is a comment */ int x; /* sectionEndExample */';
pattern='(?<=/\*)[\w\s]+(?=\*/)';
sectionEndIdx1 = regexp(str, pattern,'match');
sectionEndIdx1=sectionEndIdx1{end}
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 15 日
You are right, even a space will make a problem.
Swati Tiwari
Swati Tiwari 2013 年 8 月 16 日
Thank you very much for all your help. Really appreciate it. To rephrase the question, I wish to get the start position and the end position of the comment (including the starting '/*' and the ending '*/' that contains the string 'sectionEndExample'.

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


Swati Tiwari
Swati Tiwari 2013 年 8 月 16 日
Thank you all. I also figured out a way to do it!
str = '/* This is a sectionEndExample comment */ int x; /* sectionEndExample */';
pattern='/\*[^\*]*sectionEndExample[^\*]*\*/';
sectionEndIdx1 = regexp(str, pattern,'match')
This will find all the comments with the string sectionEndExample.
  1 件のコメント
Swati Tiwari
Swati Tiwari 2013 年 8 月 16 日
編集済み: Azzi Abdelmalek 2013 年 8 月 16 日
Here, I divided the string into three parts:
1. (anything but a *)* sectionEndExample (anything but a *)*
2. The start comment identifier /*
3. The end comment identifier

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

カテゴリ

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