MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

Simon
0

Finding specfic string within another string

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?

  0 件のコメント

サインイン to comment.

5 件の回答

回答者: 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))

  1 件のコメント

Jan
2017 年 9 月 27 日
Safer:
idx = strfind(str1, ['\', str2, '\'])

サインイン to comment.


回答者: 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
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.

サインイン to comment.


回答者: 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')

  0 件のコメント

サインイン to comment.


回答者: Cedric Wannaz
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 件のコメント

Cedric Wannaz
2017 年 9 月 27 日
Not in this direction ;-)
If he gets the path from PWD, DIR, FULLFILE, etc on a Windows machine, it will have backslashes.
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 Wannaz
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.

サインイン to comment.


回答者: 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?

  0 件のコメント

サインイン to comment.



Translated by