Regexp match a string only if it is not preceded by another string

26 ビュー (過去 30 日間)
Sacha Ruzzante
Sacha Ruzzante 2021 年 5 月 12 日
コメント済み: Sacha Ruzzante 2021 年 5 月 12 日
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

採用された回答

Walter Roberson
Walter Roberson 2021 年 5 月 12 日
pattern = '(?<!blowing )snow'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by