Last active
April 17, 2019 16:54
-
-
Save neilmayhew/e4fc90b7eaeb7bbcfeb6d6938544ecc9 to your computer and use it in GitHub Desktop.
Magicloud log parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Text.Regex.Applicative | |
(<:>) :: RE a b -> RE a [b] -> RE a [b] | |
(<:>) = liftA2 (:) | |
infixr 3 <:> | |
type Item = (String, String) | |
item :: String -> RE Char Item | |
item key = (,) key <$> few anySym | |
-- ${year}/${month}/${day} ${hour}:${minute} User ${username} runs command ${command}. | |
pattern :: RE Char [Item] | |
pattern = | |
item "year" <* "/" <:> item "month" <* "/" <:> item "day" <* " " <:> | |
item "hour" <* ":" <:> item "minute" <* " User " <:> | |
item "username" <* " runs command " <:> item "command" <* "." <:> | |
pure [] | |
input :: String | |
input = "2019/04/17 17:27 User magicloud runs command ls." | |
output :: Maybe [Item] | |
output = match pattern input | |
-- Just [("year","2019"),("month","04"),("day","17"),("hour","17"),("minute","27"),("username","magicloud"),("command","ls")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment