# Compare patterns in byte array

**URL:** https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463
**Category:** New to Julia
**Created:** [February 17, 2021, 2:29pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463 "2021-02-17T14:29:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [February 17, 2021, 2:29pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463/1 "2021-02-17T14:29:31Z")

</div>

How can I find the position in array “a”, where is pattern match from array “b”? Does standard library have implemented this functionality?

```julia
a = [0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61]

b = [0x73, 0x20, 0x69]

```

Expected result is 4, or 4:6 or similar.

---

<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: [February 17, 2021, 2:41pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463/2 "2021-02-17T14:41:18Z")

</div>

I don’t know of a built-in tool to do exactly this, but it can be built out of existing tools:

```julia
julia> function find_sub_array(needle, haystack)
         findfirst(firstindex(haystack):(lastindex(haystack) - length(needle))) do i
           @view(haystack[i:(i + length(needle) - 1)]) == needle
         end
       end
find_sub_array (generic function with 1 method)

julia> find_sub_array(b, a)
4

```

This just uses `findfirst` to find the first index `i` such that `a[i:(i + 2)] == b`. If `b` is not present, then `findfirst` will return `nothing`.

---

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [February 19, 2021, 12:34pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463/3 "2021-02-19T12:34:50Z")

</div>

Thank you. It is working. I modified your solution to get first indexes of all patterns in array:

```julia
a = [0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x70, 0x20, 0x61, 0x70, 0x20, 0x61, 0x73, 0x20, 0x69, 0x20]

b = [0x73, 0x20, 0x69]

function find_sub_array_index(pattern, arr)
    findfirst(firstindex(arr):(lastindex(arr) - length(pattern))) do i
      @view(arr[i:(i + length(pattern) - 1)]) == pattern
    end
  end

function find_sub_array_indexes(pattern, arr)
    arrResult = []
    result = 0
    resultLast = 1-size(pattern,1)
    patternSize = size(pattern,1)
    while result !== nothing
        nextIndex = (resultLast + patternSize)
        if nextIndex > size(arr, 1)
            return arrResult
        end
        result = resultLast + patternSize + find_sub_array_index(pattern, arr[nextIndex:end])

        if result === nothing
            return arrResult
        end

        push!(arrResult, result-1)
        resultLast = result
    end
end

println(find_sub_array_indexes(b, a))

```

This is not optimized solution, just working example.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 19, 2021, 1:07pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463/4 "2021-02-19T13:07:18Z")

</div>

> [@Michal](#):
>
> How can I find the position in array “a”, where is pattern match from array “b”? Does standard library have implemented this functionality?

In Julia 1.6 you can use `findfirst` and `findnext` for this:

```julia
julia> findfirst(b, a)
4:6

```

See also this discussion: [Finding position of a sequence in a UInt8 array?](https://discourse.julialang.org/t/finding-position-of-a-sequence-in-a-uint8-array/45757) which resulted in this PR: [https://github.com/JuliaLang/julia/pull/37283](https://github.com/JuliaLang/julia/pull/37283)

---

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [February 19, 2021, 2:25pm UTC](https://discourse.julialang.org/t/compare-patterns-in-byte-array/55463/5 "2021-02-19T14:25:43Z")

</div>

Ok, I’ll check it. Thank you for advice.
