Hello,
I have two patterns p1 (or) p2, to be found within a single string, str. The objective is to get the position if either of those two patterns is found. But the following doesn't seem to work:
position=strfind(str,p1 || p2)
How to modify this statement to get the result?
Thanks.

답변 (1개)

KSSV
KSSV 2021년 2월 12일

0 개 추천

position1 = strfind(str,p1) ;
position2 = strfind(str,p2) ;
OR
position = strfind(str,[p1, p2]) ;

댓글 수: 3

v k
v k 2021년 2월 12일
Thanks. But doesn't seem to work.
str='<s 123>item1<m>list unwanted<dfy 97>item2<m>';
p1='<s 123>';
p2='<dfy 97>';
p3='<m>';
startPosition=strfind(str,[p1,p2]);
startPosition=startPosition+numel(p1);
endPosition=strfind(str,p3);
endPosition=endPosition-1;
This gives
startPosition =
[]
Also, how to resolve the fact the lengths of p1 and p2 are different, and hence, there is a mismatch in the startPosition when adding numel(p*) to it?
v k
v k 2021년 2월 12일
The objective is to extract out the items: item1 and item2.
KSSV
KSSV 2021년 2월 12일
position1 = strfind(str,p1) ;
position2 = strfind(str,p2) ;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

v k
2021년 2월 12일

댓글:

2021년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by