# @match string startswith endswith

**URL:** <https://discourse.julialang.org/t/match-string-startswith-endswith/87797>\
**Category:** General Usage\
**Tags:** match\
**Created:** [September 26, 2022, 6:07am UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797 "2022-09-26T06:07:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 26, 2022, 6:07am UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797/1 "2022-09-26T06:07:52Z")

</div>

Is there a @match equivalent of this?

```julia
type = startswith(ticker,"ABC") ? "M1" :
         startswith(ticker,"X") ? "M2" :
         contains(ticker,"123") ? "M3" :
         endswith(ticker,"end") ? "M4" : missing

```

something like

```julia
type = @match ticker begin
   startswith("ABC") => "M1"
   startswith("X") => "M2"
   contains("123") => "M3"
   endswith("end") => "M4"
end

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 26, 2022, 6:15pm UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797/2 "2022-09-26T18:15:52Z")

</div>

If you’re using [Match.jl, you can do](http://kmsquire.github.io/Match.jl/latest/#Alternatives-and-Guards-1):

```julia
 type = @match ticker begin
    t, if startswith(t, "ABC") end => "M1"
    t, if startswith(t, "X") end => "M2"
    t, if contains(t, "123") end => "M3"
    t, if endswith(t, "end") end => "M4"
 end

```

[Rematch.jl allows](https://github.com/RelationalAI-oss/Rematch.jl) for the slightly nicer:

```julia
 type = @match ticker begin
    t where startswith(t, "ABC") => "M1"
    t where startswith(t, "X") => "M2"
    t where contains(t, "123") => "M3"
    t where endswith(t, "end") => "M4"
 end

```

[MLStyle.jl comes closest to the syntax in your example](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html#predicates), though it isn’t documented super well:

```julia
 type = @match ticker begin
    GuardBy(startswith("ABC")) => "M1"
    GuardBy(startswith("X")) => "M2"
    GuardBy(contains("123")) => "M3"
    GuardBy(endswith("end")) => "M4"
 end

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 28, 2022, 10:46am UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797/3 "2022-09-28T10:46:14Z")

</div>

Thanks Sundara  
I’ll have to check out MLStyle.jl  
pitty it needs that GuardBy on every line.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [October 19, 2022, 2:32am UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797/4 "2022-10-19T02:32:03Z")

</div>

MLStyle fits your use case:

```julia
test(x) = @match x begin
    Startswith{"ABC"} => "M1"
    Startswith{"X"} => "M2"
    Contains{"123"} => "M3"
    Endswith{"end"} => "M4"
    _ => error("no match")
end

julia> test("ABC")
"M1"

julia> test("X")
"M2"

julia> test("123")
"M3"

julia> test("as123")
"M3"

julia> test("as123end")
"M3"

julia> test("as12end")
"M4"

```

To use such patterns `Startswtih`, `Contains` and `Endswith`, you can define them as follows:

```julia
using MLStyle

@active Startswith{s::String}(x) begin
    x isa AbstractString && startswith(x, s)
end

@active Contains{s::String}(x) begin
    x isa AbstractString && contains(x, s)
end

@active Endswith{s::String}(x) begin
    x isa AbstractString && endswith(x, s)
end

```

FYI: [MLStyle - Active Patterns](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html#active-patterns).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [October 19, 2022, 3:53am UTC](https://discourse.julialang.org/t/match-string-startswith-endswith/87797/5 "2022-10-19T03:53:10Z")

</div>

thats really slick 🙂
