How do I find the last occurrence of a match using regexp in MATLAB?
44 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 16 de Ag. de 2013
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)
0 comentarios
Más respuestas (3)
Cedric
el 15 de Ag. de 2013
Editada: Cedric
el 15 de Ag. de 2013
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 '
0 comentarios
Azzi Abdelmalek
el 15 de Ag. de 2013
str = '/* This is a comment */ int x; /* sectionEndExample */';
pattern='(?<=/\*)[\w\s]+(?=\*/)';
sectionEndIdx1 = regexp(str, pattern,'match');
sectionEndIdx1=sectionEndIdx1{end}
3 comentarios
Swati Tiwari
el 16 de Ag. de 2013
1 comentario
Swati Tiwari
el 16 de Ag. de 2013
Editada: Azzi Abdelmalek
el 16 de Ag. de 2013
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!