Finding specfic string within another string

조회 수: 1 (최근 30일)
Simon
Simon 2017년 9월 18일
답변: Lucas Silva 2018년 11월 26일
I am having trouble finding a specific part of a file path. The path is something along: \\blabla\blabla\blabla\blabla\blabla\blabla\04 - Black\05 - TEST
I need to find "04 - Black" in the path name. The name is changing and so is its position in the path - Therefor I need to find the first instance of 'XX - '. Afterwards I need to find the "05 - TEST" folder, which is also changing name and position.
Can anyone help?

답변 (5개)

KSSV
KSSV 2017년 9월 18일
편집: KSSV 2017년 9월 18일
str1 = '\\blabla\blabla\blabla\blabla\blabla\blabla\04 - Black\05 - TEST';
str2 = '04 - Black' ;
idx = strfind(str1,str2)
str1(idx:(idx+length(str2)-1))

José-Luis
José-Luis 2017년 9월 18일
yourStr = '\blabla\blabla\blabla\blabla\blabla\blabla\04 - Black\05 - TEST'
re = '\\[0-9]{2}\s-\s[a-zA-Z]+';
result = regexp(yourStr,re,'match');
  댓글 수: 1
Guillaume
Guillaume 2017년 9월 18일
Other possible regex, which is more permissive:
re = '\\[0-9]{2}\s-\s[^\\]+';
Allows anything that is not \ after the two digits, whitespaces, and dash.

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


Guillaume
Guillaume 2017년 9월 18일
if the aim is to obtain the name of the folder after the 'XX - ', then:
folder = regexp(path, '(?<=\\[0-9]{2}\s-\s[^\\]+\\)[^\\]+', 'match', 'once')

Cedric
Cedric 2017년 9월 18일
Why not simply
>> str = '\\blabla\blabla\blabla\blabla\blabla\blabla\04 - Black\05 - TEST' ;
>> folders = regexp( str, '\d\d - [^\\]+', 'match' )
folders =
1×2 cell array
'04 - Black' '05 - TEST'
  댓글 수: 5
Image Analyst
Image Analyst 2017년 10월 8일
Those functions may return backslashes, but you can use forward slashes in your strings that you make up and send into functions like that with no problems. Like Stephen says, Windows accepts either forward or backward slashes equally well.
Cedric
Cedric 2017년 10월 8일
편집: Cedric 2017년 10월 8일
Yes but we need to match a sub-string in this context and for this he needs to use the correct separator in the pattern, for REGEXP to work. He could use both for ending the match by defining the pattern as '\d\d - [^\\/]+'.
But again, if there is no problem in the "other direction" (because MATLAB can deal with either filesep), here both possibilities have to be managed, either by building the pattern using filesep or by making the pattern flexible enough.

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


Lucas Silva
Lucas Silva 2018년 11월 26일
Eu tenho que fazer um processo semelhante, mas o método de separar as seqüências de caracteres é o ';' Eu tentei identificar o; como se fosse parte do comando, mas o matlab não aceita como.
Existe alguma maneira de obter essa informação?

카테고리

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