# Local Pattern Matching

**URL:** <https://discourse.elm-lang.org/t/local-pattern-matching/5428>\
**Category:** Learn\
**Created:** [April 3, 2020, 4:34pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428 "2020-04-03T16:34:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hanifhefaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hanifhefaz/32/3094_2.png) [@hanifhefaz](https://discourse.elm-lang.org/u/hanifhefaz)\
**Post date:** [April 3, 2020, 4:34pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/1 "2020-04-03T16:34:22Z")

</div>

Hello, I am a beginner to Elm, and I want to implement an idea which is so simple. I want to save some sentences in my model, then I want to look for a user’s given word in these sentences. if the word matches, then return a list of all of those sentences. I want those returned sentences to be clickable to view more details and complete sentences.

Is this simply possible in elm? If yes give me a simple Hint and I will start figuring it out. and if I faced a problem, i will ask it here.

Example: I am looking for word ‘this’ and it should return all the sentences contain this.  
 ![Capture](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/1/1a3cec5bdd3af78730d0f4b4e5e3e4f404f49a6b.png)

Thank You.

---

<div class="post-metadata">

**Author:** ![francescortiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/francescortiz/32/1721_2.png) [@francescortiz](https://discourse.elm-lang.org/u/francescortiz)\
**Post date:** [April 3, 2020, 8:57pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/2 "2020-04-03T20:57:04Z")

</div>

IT os perfectly possible in elm. The are many ways to address your situation. The simpler approach would be to replace the matches with some html and give the output string to the html-parser package.

A more advanced approach would use a custom parser instead of of the buildHtml -\> html-parser process. This could allow you to give you custom rendering and more contol over the events, and without the need of having to first do a replacement.

---

<div class="post-metadata">

**Author:** ![paulh](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paulh/32/5022_2.png) [@paulh](https://discourse.elm-lang.org/u/paulh)\
**Post date:** [April 3, 2020, 9:03pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/3 "2020-04-03T21:03:51Z")

</div>

Hi,

Welcome to Elm, and yes, it certainly is possible with Elm.

If you store your sentences as a `List` of `String`s on your Model, then you can combine `List.filter` and `String.contains` to extract the relevant sentences.

You should then end up with another `List`, either empty for no matches, or with one or more items that did match. You can then use `List.map` in your view in order to display the results.

That should get you started, if you want full code examples just shout.

HTH

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [April 4, 2020, 10:16pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/4 "2020-04-04T22:16:48Z")

</div>

I have a project that has code in it that might help you:

> **[the-sett/elm-swagger](https://github.com/the-sett/elm-swagger/tree/master/example)**
>
> Tools for building Swagger specifications in Elm, and for generating Elm code from them. - the-sett/elm-swagger

This makes use of a ‘trie’ data structure to index all of the text in an OpenAPI model, which makes it searchable. You may not need this level of sophistication, depending on what kind of search you are implementing. For example, if its always whole words, you could just put all the words in a `Set` or `Dict`. The trie is useful for more flexible sub-string matching:

[https://package.elm-lang.org/packages/elm-scotland/elm-tries/latest/](https://package.elm-lang.org/packages/elm-scotland/elm-tries/latest/)

Once you have found the relevant sentences, this is for highlighting text in a string in the HTML:

> <https://github.com/the-sett/elm-swagger/blob/master/example/src/elm/ViewUtils.elm#L57>

---

<div class="post-metadata">

**Author:** ![hanifhefaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hanifhefaz/32/3094_2.png) [@hanifhefaz](https://discourse.elm-lang.org/u/hanifhefaz)\
**Post date:** [April 5, 2020, 5:45pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/6 "2020-04-05T17:45:11Z")

</div>

I would love to see an example. I appreciate it…

---

<div class="post-metadata">

**Author:** ![hanifhefaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hanifhefaz/32/3094_2.png) [@hanifhefaz](https://discourse.elm-lang.org/u/hanifhefaz)\
**Post date:** [April 5, 2020, 5:49pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/7 "2020-04-05T17:49:41Z")

</div>

Nice! I will play with it.  
What I am going to do is (may be its stupid), that i want to have chatbot in my elm website. the chat bot will pop up when the user access the website and then asks the users to write any help word they are looking for. for example: the user types “privacy” and the bot should return any sentences where privacy is written from the model.

---

<div class="post-metadata">

**Author:** ![paulh](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paulh/32/5022_2.png) [@paulh](https://discourse.elm-lang.org/u/paulh)\
**Post date:** [April 5, 2020, 6:09pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/8 "2020-04-05T18:09:48Z")

</div>

No problem, here you go…

```auto

type alias Model =
  { sentences : List String
  , results : List String
  }

sentences : List String
sentences =
  [ "Hello there this is our first sentence."
  , "this is the second sentence."
  , "there is no third number for the sentence."
  , "and this is the fourth one."
  ]

initModel : Model
  { sentences = sentences 
  , results = []
  }

type Msg
  = WordEntered String

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    WordEntered word ->
      ( { model
          | results =
            model.sentences
              |> List.filter (String.contains word)
         }
      , Cmd.none
      )

view : Model -> Html Msg
view model =
  let
    results =
      model.results
        |> List.map
          (\ result -> 
                -- wrap your result in some html
          )
  in
  -- Your view code with the results embedded however you need

```

Obviously it’s not a full working example that will compile, but it should get you going. There are other ways, as noted by other replies, but I think this is a simple way of getting started.

HTH

---

<div class="post-metadata">

**Author:** ![hanifhefaz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hanifhefaz/32/3094_2.png) [@hanifhefaz](https://discourse.elm-lang.org/u/hanifhefaz)\
**Post date:** [April 5, 2020, 6:20pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/9 "2020-04-05T18:20:18Z")

</div>

**Thank You.**  
Can you please make a simple view for this as well. Just not to make things missed. so it will be a functional demo.

---

<div class="post-metadata">

**Author:** ![paulh](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/paulh/32/5022_2.png) [@paulh](https://discourse.elm-lang.org/u/paulh)\
**Post date:** [April 5, 2020, 6:32pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/10 "2020-04-05T18:32:43Z")

</div>

Take a look here: [https://package.elm-lang.org/packages/elm/html/latest/](https://package.elm-lang.org/packages/elm/html/latest/) it has all the information you need to build a view. Also, spend some time going through the official guide.

I’m not brushing you off, but **you’ll** be better off in the long run if you can figure some of this out yourself.

Try some things out, experiment, (have fun !!!), and if you get stuck, post your code here and we can help you put it right and show/explain what you did wrong.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [April 15, 2020, 6:32pm UTC](https://discourse.elm-lang.org/t/local-pattern-matching/5428/11 "2020-04-15T18:32:47Z")

</div>

Closed on April 15, 2020, 6:32pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
