# Correct usage of regex matches

**URL:** <https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796>\
**Category:** New to Julia\
**Tags:** regex\
**Created:** [May 8, 2021, 8:19pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796 "2021-05-08T20:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [May 8, 2021, 8:19pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/1 "2021-05-08T20:19:53Z")

</div>

Hey, I am quite new to Julia and come from an R background. I was wondering what the correct (or maybe short/julian way) to solve a simple regex matching problem would be. I have included a minimal working example below, but the solutions seems complicated to me. My main gripe is that I did not find a solution to access the matches (m.matches) in a vectorized manner (I expected some method to be available when I call m.matches where m is a vector of regex matches).

```nohighlight
my_strings = ["anystring","question1","question29","somestring","someotherstring"]

m = match.(r"question[0-9]+",my_strings)

m = filter(x -> !isnothing(x), m)

my_result = [m[i].match for i in 1:length(m)]
```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [May 8, 2021, 8:56pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/2 "2021-05-08T20:56:48Z")

</div>

I can only think of a very slight improvement

`filter(x->!isnothing(x),m1) .|> (x->x.match)`

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 8, 2021, 10:21pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/3 "2021-05-08T22:21:47Z")

</div>

Could use an array comprehension with an `if` inside

```julia
julia> my_strings = ["anystring","question1","question29","somestring","someotherstring"];

julia> r = r"question[0-9]+";

julia> [x.match for x in (match(r, m) for m in my_strings) if x !== nothing]
2-element Vector{SubString{String}}:
 "question1"
 "question29

```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [May 8, 2021, 10:27pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/4 "2021-05-08T22:27:40Z")

</div>

> [@purplishrock](#):
>
> ```julia
> x->!isnothing(x)
> 
> ```

can be written `!isnothing`.

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [May 8, 2021, 11:22pm UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/5 "2021-05-08T23:22:05Z")

</div>

Thanks for the comments! I really like both the array comprehension and the vector pipe with the anonymous function, but would probably prefer the latter since it seems a bit more readable to me (at the moment). But I will certainly try to find more use cases for array comprehensions in my code in the future!

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [May 9, 2021, 12:19am UTC](https://discourse.julialang.org/t/correct-usage-of-regex-matches/60796/6 "2021-05-09T00:19:26Z")

</div>

Personally I would prefer `findfirst`:

```julia
filter(my_strings) do s
    !isnothing(findfirst(r"^question[0-9]+$", s))
end

```
