regexp find next char
Afficher commentaires plus anciens
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)
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}
1 commentaire
Mjan88
le 5 Oct 2018
Sean de Wolski
le 5 Oct 2018
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!