Should I use a Parser?

Hello!

Given:

text : String
text = "this is some text with some-id 123 and this is the object-id 159 and some other text and one person-id 89 and so on..."

type alias Extracted =
    { beforeObjectId: String
    , objectId: Int
    , beforePersonId: String
    , personId: Int
    , afterPersonId: String
    }

I want to parse str to build Extracted.

My current approach is to find “object-id” (String.find), split it, parse the Int, and do the same for the person-id. But maybe this can be done with a parser? But I haven’t found a solution on how to parse until a given keyword (object-id).

Regards,
Niko

I believe that chompWhile, chompUntil and similar functions will help you here.

1 Like

I totally missed chompUntil. Thank you!

parser : Parser.Parser Extracted
parser =
    succeed Extracted
        |= (getChompedString <| chompUntil "object-id")
        |. keyword "object-id"
        |. spaces
        |= int
        |. spaces
        |= (getChompedString <| chompUntil "person-id")
        |. keyword "person-id"
        |. spaces
        |= int
        |. spaces
        |= (getChompedString <| chompWhile (always True))
3 Likes

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