Hello, I have some problem with understanding regexp expression
I have some names: ["T_24_UZK500.txt"; "FWD_T80_UZK500.txt"; "T80_UZK700.txt"]
how can I get numbers after "T" and after "UZK"?
I need a rule that will describe only the numbers after the designated patterns.

2 件のコメント

Adam Danz
Adam Danz 2019 年 5 月 10 日
Are "T" and "UZK" the only possible letters in the names?
Eduard Mazur
Eduard Mazur 2019 年 5 月 10 日
編集済み: Eduard Mazur 2019 年 5 月 10 日
It's variable symbols which are defined earlier.
I mean, I have filenames (string) and patterns (variable inside this names). With this information I need extract numerical vallues after variables..

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

 採用された回答

Stephen23
Stephen23 2019 年 5 月 10 日
編集済み: Stephen23 2019 年 5 月 10 日

1 投票

Matching only integer numbers after 'UZK' or 'T_' (it is unclear in your question if the underscore is permitted or not, but the regular expression below is easy to adapt):
>> S = {'T_24_UZK500.txt';'FWD_T80_UZK500.txt';'T80_UZK700.txt'};
>> C = regexp(S,'(?<=(T_?|UZK))\d+','match');
>> C{:}
ans =
'24' '500'
ans =
'80' '500'
ans =
'80' '700'
Or simply by matching any integer numbers:
>> C = regexp(S,'\d+','match');
>> C{:}
ans =
'24' '500'
ans =
'80' '500'
ans =
'80' '700'

4 件のコメント

Eduard Mazur
Eduard Mazur 2019 年 5 月 10 日
編集済み: Eduard Mazur 2019 年 5 月 10 日
Thank you Stephen for solution,
it's working good,
but can we turn 'T' and 'UZK' inside in a variable?
I want to try it with dynamic field names..
Adam Danz
Adam Danz 2019 年 5 月 10 日
編集済み: Adam Danz 2019 年 5 月 10 日
Small change from Stephen's solution: here you can list the potential patterns and the expression will be created according to your list. Also, underscores are removed ahead of time.
S = {'T_24_UZK500.txt';'FWD_T80_UZK500.txt';'T80_UZK700.txt'};
patterns = {'T', 'UZK'};
exp = ['(?<=(', strjoin(patterns,'|') ,'))\d+'];
% Remove all underscores (if any)
S = strrep(S, '_', '');
C = regexp(S, exp, 'match'); % use regexpi() to ignore case
Eduard Mazur
Eduard Mazur 2019 年 5 月 10 日
Thanks for your helping!
This solution fit for me.
madhan ravi
madhan ravi 2019 年 5 月 10 日
Using exp as a variable name is not a good idea it would thwart the inbuilt function exp()

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

その他の回答 (0 件)

カテゴリ

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

製品

リリース

R2018b

質問済み:

2019 年 5 月 10 日

編集済み:

2019 年 5 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by