Getting a list of file names

I’ve been asked to convert some code from Matlab to Julia, and I am stuck right at the beginning with some Regex issues

I want the equivalent of

file_name = dir(‘A*.mmode’)
file_name = {file_name.name}

In Matlab, this will make me a matrix of however many files with the appropriate start and end bits.

I saw the following code to search for files:

searchdir(path,key) = filter(x->occursin(key,x), readdir(path))

But when I run

julia> file_name = searchdir(pwd(),^(A.+iq.mmode)$)
ERROR: syntax: invalid identifier name “.”

Any suggestions?

Try Glob.jl, which implements POSIX-compliant filename matching.

4 Likes

yous readdir() and perform regex on that list of strings.

I can maybe be wrong but did you put the regex inside a string? In Julia regexes are strings prefixed by r, see the relevant section of the manual.

3 Likes

Thank you!!
file_name = searchdir(pwd(),r"^(A.+iq.mmode)$") worked.