Local Pattern Matching

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.

Thank You.

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.

1 Like


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

If you store your sentences as a List of Strings 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.



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

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:


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

1 Like

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

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.

No problem, here you go…

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 =
              |> List.filter (String.contains word)
      , Cmd.none

view : Model -> Html Msg
view model =
    results =
        |> List.map
          (\ result -> 
                -- wrap your result in some html
  -- 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.


1 Like

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.

Take a look here: 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.


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