필터 지우기
필터 지우기

regexp: only strings without given end string

조회 수: 8 (최근 30일)
Jette
Jette 2018년 1월 29일
답변: Jyotish Robin 2018년 2월 8일
I try to use regexp to extract strings with certain conditions within a cell array.
My conditions are:
  1. String starts with one of severeal given strings (e.g. 'abc', 'def' )
  2. There can be several substrings starting with '_' following the substring defined in 1
  3. The string must not end with '_std'
Example:
header = {'abc', 'abc_b1', 'abc_b1_std', 'abc_b1_b2', 'abc_b1_b2_std', 'def_b1', 'def_b1_std', 'bcd'};
I'd like to extract:
'abc', 'abc_b1', 'abc_b1_b2', 'def_b1'
I tried the following
res = regexp( header, ['^(abc|def)(_.*)*'] );
but I don't know how to exclude the '_std' at the end.
Any ideas?
( I use MATLAB R2015b)

답변 (1개)

Jyotish Robin
Jyotish Robin 2018년 2월 8일
Hi Jette!
Your approach seems right to me in taking into consideration the first two of your requirements. Now, to extend it to exclude the '_std' at the end, you can make use of Lookaround Assertions.
To be specific,
>>res= regexp( header, ['^(abc|def)(_.*)*(?<!std)$'], 'match' )
seems like the required command you need to use.
Hope this helps!
Thanks,
Jyotish

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by