wilcard usage with strncmp

1 visualización (últimos 30 días)
sermet OGUTCU
sermet OGUTCU el 16 de Mayo de 2021
Comentada: sermet OGUTCU el 16 de Mayo de 2021
I have a cell array data (C). I use the following code to find specific character in the C;
find_character = strncmp(C, '* 2020', 7);
How can I use wilcard using strncmp to find all patterns satisfy * [0-9][0-9][0-9][0-9]? For example, if * 1988 or * 2000 exist in C, they can be also extracted.

Respuesta aceptada

Stephen23
Stephen23 el 16 de Mayo de 2021
C = {'hello','* 2020','world','* 1923'};
X = ~cellfun(@isempty,regexp(C,'^\* \d{4}$'))
X = 1×4 logical array
0 1 0 1
D = C(X)
D = 1×2 cell array
{'* 2020'} {'* 1923'}
  4 comentarios
Stephen23
Stephen23 el 16 de Mayo de 2021
"How I can modify the above codes to work consistently with my cell array."
Remove the '$' from the end of the regular expression:
C = {'hello','* 2020 3 28','world','* 1923'};
X = ~cellfun(@isempty,regexp(C,'^\* \d{4}'))
X = 1×4 logical array
0 1 0 1
sermet OGUTCU
sermet OGUTCU el 16 de Mayo de 2021
Thank you very much Stephen.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 16 de Mayo de 2021
Use the pattern functionality. I think it's easier than regexp().
>> doc pattern

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by