Regexp match a string only if it is not preceded by another string
Mostrar comentarios más antiguos
Hello,
I am working with observations of weather data and I need to use regexp to match the word 'snow' only if it is NOT preceded by the word 'blowing'.
I have included a MWE below. My goal is to create a logical mask that will identify the correct cells. I want to include strings like 'Snow', 'Moderate Snow', and 'Snow,Blowing Dust'. However, I want to exclude 'Blowing Snow' and 'Freezing Rain,Blowing Snow'. Lastly, I want to include cell 6 'Snow,Blowing Snow' because it includes one instance of the string 'snow' that is not preceded by the string 'blowing'.
I'm not great with regexp so I'm wondering if someone is more clever than me.
WEATHER={'Snow';...
'Blowing Snow';...
'Cloudy';...
'Moderate Snow';...
'Freezing Drizzle';...
'Snow,Blowing Snow';...
'Snow,Smoke';...
'Snow,Blowing Dust';...
'Freezing Rain,Blowing Snow'}
match_Snow=regexpi(WEATHER,'\<(?!blowing snow\>)snow');
mask_snow=~cellfun(@isempty, match_Snow);
% desired output:
% mask_snow =
%
% 9×1 logical array
%
% 1
% 0
% 0
% 1
% 0
% 1
% 1
% 1
% 0
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!