How do I find the last occurrence of a match using regexp in MATLAB?
古いコメントを表示
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 件のコメント
Azzi Abdelmalek
2013 年 8 月 15 日
if you knpw the comment sectionEndExample, why are you looking for it?
Or do you want to find the last comment between * ?
Swati Tiwari
2013 年 8 月 16 日
Azzi Abdelmalek
2013 年 8 月 16 日
Ok, but what about the answers below?
採用された回答
その他の回答 (3 件)
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
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
2013 年 8 月 15 日
You are right, even a space will make a problem.
Swati Tiwari
2013 年 8 月 16 日
Swati Tiwari
2013 年 8 月 16 日
1 件のコメント
Swati Tiwari
2013 年 8 月 16 日
編集済み: Azzi Abdelmalek
2013 年 8 月 16 日
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!