This should a fairly straightforward problem. I have a cell array of strings. I want to extract a part of the string with REGEXP. My string looks like the following: str= 'R44821_181026120003i3t001B02f05d1.TIF' or str = R44821_181102140001i3D03f01d1.TIF I want to extract the capital letter from the string (barring the first letter) followed by two numbers such that the output from the above two strings are B02 or D03. I tried the following code :
regexp(fileNames{1}, '(\w+)[A-Z](..)(\D|[^A-Z])', 'tokens', 'once')
But I can't seem to extract the Capital letter associated with the number

 採用された回答

Stephen23
Stephen23 2018 年 11 月 6 日
編集済み: Stephen23 2018 年 11 月 6 日

0 投票

Use a look-around operation to ensure that the previous character was alphanumeric:
>> C = {'R44821_181026120003i3t001B02f05d1.TIF','R44821_181102140001i3D03f01d1.TIF'};
>> D = regexp(C,'(?<=\w)[A-Z]\d{2}','match','once');
>> D{:}
ans = B02
ans = D03
Or a look-around to ensure that it does not match the start of the string:
D = regexp(C,'(?<!^)[A-Z]\d{2}','match','once')

1 件のコメント

Himanish Basu
Himanish Basu 2018 年 11 月 6 日
Worked! Thanks a ton!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by