# Help me make a regex, instead of this?

**URL:** <https://discourse.julialang.org/t/help-me-make-a-regex-instead-of-this/23878>\
**Category:** General Usage\
**Tags:** strings, regex\
**Created:** [May 5, 2019, 5:03pm UTC](https://discourse.julialang.org/t/help-me-make-a-regex-instead-of-this/23878 "2019-05-05T17:03:49Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![natemcintosh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natemcintosh/32/9021_2.png) [@natemcintosh](https://discourse.julialang.org/u/natemcintosh)\
**Post date:** [May 5, 2019, 6:57pm UTC](https://discourse.julialang.org/t/help-me-make-a-regex-instead-of-this/23878/4 "2019-05-05T18:57:11Z")

</div>

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

```julia
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](https://discourse.julialang.org/t/help-reading-code-warntype-output/20832).

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`](https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.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](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized-1).

Finally, let’s single out all the unique names with the [unique](https://docs.julialang.org/en/v1/base/collections/#Base.unique) function.

---

_[View the full topic](https://discourse.julialang.org/t/help-me-make-a-regex-instead-of-this/23878)._
