Regexp for only numbers and "dots"

323 ビュー (過去 30 日間)
Dale Black
Dale Black 2021 年 3 月 11 日
コメント済み: Dale Black 2021 年 3 月 13 日
I have filenames like "999.999.1.20020318.133002.0" and I want to look through folders and move files with this naming scheme (only numbers and periods). Any idea how this could be done? I tried something like strcmp and contains but I cannot figure out how to get it to work.
Thanks in advance!

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 3 月 11 日
You can use a regex like the following. this one will catch all digits and dots. The ^ in beginning and $ at the end ensure that it matches the entire name.
a = {'somname.txt' '999.999.1.20020318.133002.0'};
match = ~cellfun(@isempty,regexp(a,'^[\.0-9]*$'));
% you can use it with dir to list all files in the folder
b = dir();
match = ~cellfun(@isempty,regexp({b.name},'^[\.0-9]*$'));
  4 件のコメント
Mohammad Sami
Mohammad Sami 2021 年 3 月 11 日
編集済み: Mohammad Sami 2021 年 3 月 11 日
As noted in Walter's comment, dir will contain '.' and '..' as entries.
Assuming that first character must always be a digit, you can change the regex expression as follows.
b = dir();
match = ~cellfun(@isempty,regexp({b.name},'^\d[\.\d]*$'));
% substituted \d for 0-9
Also if you R2020b, you can use the new pattern functionality as follows.
b = dir();
match = matches({b.name},regexpPattern('^\d[\.\d]*$'));
Dale Black
Dale Black 2021 年 3 月 13 日
Thank you. I don't have the newest version so I didn't try that but the first option works and avoided the .. . problem.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by