I have the following string:
A = p:\A to B\Mat Lab\Ques Tion\
And I'm trying to extract the B = 'Mat Lab' and C = 'Ques Tion'.
The '\A to B\' has always the same structure, namely '\o{134}[A-Z]\sto\s[A-Z]\o{134} in regexp (\o{134} is '\')
I want to find B and C. They can be one word and multiple words. Does anybody know, why below isn't working? (it is working when B='Matlab' and C='Question' though)
[start_id,end_id] = regexp(A,'\o{134}[A-Z]\sto\s[A-Z]\o{134}\w*(?=\o{134})\o{134}\w*\w*(?=\o{134})');
B = char(extractBefore(dirname(start_id+8:end_id),'\'));
C = char(extractAfter(dirname(start_id+8:end_id),'\'));

답변 (2개)

jonas
jonas 2018년 10월 5일
편집: jonas 2018년 10월 5일

0 개 추천

No real benefit in using regexp here
strs = strsplit(A,'\')
id = find(strcmp(strs,'A to B')==true)
B = strs{id+1}
C = strs{id+2}
Sean de Wolski
Sean de Wolski 2018년 10월 5일

0 개 추천

Use string arrays!
>> a = string('A = p:\A to B\Mat Lab\Ques Tion\')
a =
"A = p:\A to B\Mat Lab\Ques Tion\"
>> as = split(a, "\")
as =
5×1 string array
"A = p:"
"A to B"
"Mat Lab"
"Ques Tion"
""
>> a2b = find(contains(as, "A to B"))
a2b =
2
>> ccdd = as(a2b+[1 2])
ccdd =
2×1 string array
"Mat Lab"
"Ques Tion"

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 10월 5일

답변:

2018년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by