# Find index of all occurences of a string

**URL:** <https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044>\
**Category:** New to Julia\
**Created:** [April 11, 2019, 4:25pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044 "2019-04-11T16:25:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2019, 4:25pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/1 "2019-04-11T16:25:19Z")

</div>

Hey guys!

Say I have a string like:

> “aaabbbaaabbbaaabbb”

Then I want to find the index of what corresponds to “aaa” so I would expect to find something like:

> index = [1:3,7:9,13:15]

I know that “findfirst” exists and I could just make a subfunction utilizing this, just wondered if there was an easier approach.

Kind regards

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 11, 2019, 5:13pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/2 "2019-04-11T17:13:41Z")

</div>

One option would be to use a regular expression. You can provide a third argument to `match()` instructing the search to start from a particular position in the string, which you can use to find sequential matches like so:

```julia
julia> pattern = r"aaa" # the r"" prefix makes this a regular expression
r"aaa"

julia> target = "aaabbbaaabbbaaabbb"
"aaabbbaaabbbaaabbb"

julia> m = match(pattern, target)
RegexMatch("aaa")

julia> m.offset
1

julia> m = match(pattern, target, m.offset + 1)
RegexMatch("aaa")

julia> m.offset
7

julia> m = match(pattern, target, m.offset + 1)
RegexMatch("aaa")

julia> m.offset
13

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2019, 5:28pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/3 "2019-04-11T17:28:52Z")

</div>

Thanks, now I can try benchmarking both functions, I will have to make a for loop it seems, if my keyword appears multiple times.

Kind regards

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 11, 2019, 5:35pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/4 "2019-04-11T17:35:48Z")

</div>

Yup, exactly 🙂

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 13, 2019, 8:40pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/5 "2019-04-13T20:40:23Z")

</div>

In theory `findall("aaa", "aaabbbaaabbbaaabbb")` should probably do what you request. That would be consistent with `findfirst` and friends. Feel free to file a feature request.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 13, 2019, 9:09pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/6 "2019-04-13T21:09:53Z")

</div>

Will do so tomorrow, I also wondered why it didn’t.

Kind regards

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [April 14, 2019, 10:15pm UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/7 "2019-04-14T22:15:00Z")

</div>

I think it used to, or there was some similar function that did. There was [a major refactor](https://github.com/JuliaLang/julia/issues/10593) of `search` and `find*` functions before the release of 1.0, you’ll probably find something about this in that issue or related PRs.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [April 15, 2019, 5:51am UTC](https://discourse.julialang.org/t/find-index-of-all-occurences-of-a-string/23044/8 "2019-04-15T05:51:48Z")

</div>

How about using `eachmatch` ? (I guess it’s a natural next step from @rdeits’s suggestion)

```julia
julia> s = "aaabbbaaabbbaaabbb"
julia> range(m::RegexMatch) = m.offset .+ (0:length(m.match)-1)
julia> [range(e) for e ∈ eachmatch(r"aaa", s)]
 1:3  
 7:9  
 13:15

```

**Edit** : if done over characters that may not have length one, you’d have to adjust `range` to something like `0:prevind(m.match, lastindex(m.match))`
