MATLAB: How to find the last occurrence of a match using regexp in MATLAB

regexp last match

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 🙁

Best Answer

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)