regexp find next char

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),'\'));

Réponses (2)

jonas
jonas le 5 Oct 2018
Modifié(e) : jonas le 5 Oct 2018

0 votes

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 le 5 Oct 2018

0 votes

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"

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by