Find within string, location where space doesn't occur before upper (capital) letter

11 visualizaciones (últimos 30 días)
I have multiple strings from excel file and would like to do the following.
string='GeneralCase and UpperManagement'
So I would like to extract first
'General'
then
'Case and Upper'
then
'Management'
Is this possible using regexp?
Thanks in advance for your help

Respuesta aceptada

Thorsten
Thorsten el 21 de Nov. de 2014
To get the indices
ind = regexp(string, '(^|[^ ])[A-Z]');
To extract the strings from the indices
L{1} = string(1:ind(2));
for i=2:numel(ind)-1
L{end+1} = string(ind(i)+1:ind(i+1));
end
if numel(ind) > 1
L{end+1} = string(ind(end)+1:end);
end

Más respuestas (1)

Stephen23
Stephen23 el 21 de Nov. de 2014
A simpler solution can be achieved in one line using regexp, and without using any loops or non-preallocated cell arrays:
>> string = 'GeneralCase and UpperManagement';
>> regexp(string,'.*?[a-z](?=[A-Z]|$)','match')
ans =
'General' 'Case and Upper' 'Management'
Note that regexp also allows cell arrays of strings as it input, so you process large numbers of strings in one go using this method.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by