Find exact string match in a cell of strings
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Milan Prakash
el 7 de Mayo de 2022
Comentada: Milan Prakash
el 7 de Mayo de 2022
Hello,
I would like to match a string in a cell array of strings.
For example I want to do exact match of str
str = 'XYZ1'
in a cell array of C
C = {'This is XYZ1','This is XYZ11', 'This is XYZ1111','This is XYZ1.0'}
Problem with using regexp -
fun = @(x) regexp(x,'XYZ1','match')
fidx = cellfun(fun, C)
or contains -
fun = @(x) contains(x,'XYZ1')
fidx = cellfun(fun, C)
is that all four cell strings contains some version of the string 'XYZ1' I am looking for. Kindly let me know if I can change something with regexp (there is help but its bit confusing for me with respect to using conditions/tokens etc.) or anything else I can use.
Regards,
Milan
2 comentarios
Respuesta aceptada
Stephen23
el 7 de Mayo de 2022
C = {'This is XYZ1','This is XYZ11', 'This is XYZ1111','This is XYZ1.0'};
str = 'XYZ1';
rgx = sprintf('%s(?=$|\\s)',str);
regexp(C,rgx,'match','once')
Más respuestas (0)
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!