MATLAB Answers

Regexp for only numbers and "dots"

13 views (last 30 days)
Dale Black
Dale Black on 11 Mar 2021
Commented: Dale Black on 13 Mar 2021
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!

Accepted Answer

Mohammad Sami
Mohammad Sami on 11 Mar 2021
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 Comments
Dale Black
Dale Black on 13 Mar 2021
Thank you. I don't have the newest version so I didn't try that but the first option works and avoided the .. . problem.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!