Help me make a regex, instead of this?

Hi Ahmed!
Another way to do it might be along the lines of something like this

function glob_thing(pattern, to_search::AbstractVector)::AbstractVector
    filter(x -> occursin(pattern, x), to_search)
end

function name_beginning(name::AbstractString)
    names_no_extension = splitext(name)[1]
    first_part = split(names_no_extension, "_")[1]
    return first_part
end

files = readdir("/path/to/your/data/")
vtk_files = glob_thing(".vtk", files)
name_beginnings = name_beginning.(vtk_files)
unique(name_beginnings)

The first function glob_thing, filters down the data from files to only those ending with “.vtk”. I named it glob_thing because I’m using it to do something similar to the glob function from terminals. I got this function from @tkoolen, who helped me out with one of my past questions.

Now that we have only files that end in “.vtk”, let’s remove everything in the names after the first underscore character. We do this wit the name_beginning() function. This uses splitext to split file names into everything before the file extension, and everything after. Then we split everything before the file extension on the “_” character, and return the first element, first_part.

My favorite part of this solution is using dot notation to apply name_beginning() on all the files in vtk_files. For more on how this works, see this page.

Finally, let’s single out all the unique names with the unique function.

1 Like