exercism/elixir/log-parser/lib/log_parser.ex

24 lines
602 B
Elixir
Raw Normal View History

2024-03-10 08:09:28 +00:00
defmodule LogParser do
def valid_line?(line) do
line =~ ~r/^\[DEBUG|INFO|WARNING|ERROR\]/u
end
def split_line(line), do: String.split(line, ~r/\<[\~\*\=\-]*\>/i)
def remove_artifacts(line), do: String.replace(line, ~r/end-of-line\d+/i, "")
def tag_with_user_name(line) do
if String.contains?(line, "User") do
map =
Regex.named_captures(
~r/^(?<type>\[\p{Lu}+\])(?<message>[\w\s]+User[\s\n]+(?<user>\S+)[\s\n]*.*)$/,
line
)
Enum.join(["[USER]", map["user"], map["type"] <> map["message"]], " ")
else
line
end
end
end