Why is this given nill

Hello,

I have this code :

(ns log-levels
  (:require [clojure.string :as str]))

(defn parts
   "Divide a text into there parts with regex"
    [s]
    (re-find #"\[<(.*)>\]: <(.*)>" s));
    
(defn message
  "Takes a string representing a log line
   and returns its message with whitespace trimmed."
  [s]
  (str/trim (last (str/split s #":")))) 

(defn log-level
  "Takes a string representing a log line
   and returns its level in lower-case."
  [s]
  (parts s))

but why is the log-level given nill instead of someting like this ["error', "you are a fool"]
when I call it like this '(loglevel [“error” : “you are a fool”])

In your example of how you call log-level, you pass it a vector. But it must be a string to work properly.

Also note that even if you pass it "error: you're a fool", it still won’t match because that RegEx requires both the square and the angle brackets, and the latter must go around both the level and the message.

I’d suggest building things from smaller to larger while constantly making sure that everything works in a REPL. E.g. I would test that re-find on some message immediately upon coming up with a RegEx that I think should work.

Sorry,
Gave the wrong string.

This is one of the tests :

(deftest ^{:task 2} log-level-error-test
  (is (= "error" (log-levels/log-level "[ERROR]: Disk full"))))

so im sure the regex is right.

It doesn’t seem correct to me:

Clojure 1.11.1
user=> (re-find #"\[<(.*)>\]: <(.*)>" "[ERROR]: Disk full")
nil
user=>

Why do you have <> there?

1 Like

Thanks,

At one way I thought I needed them.
This is solving the exercism problem

(ns log-levels
  (:require [clojure.string :as str]))

(defn parts
   "Divide a text into there parts with regex"
    [s]
    (re-find #"\[(.*)\]: (.*)" s));
    
(defn message
  "Takes a string representing a log line
   and returns its message with whitespace trimmed."
  [s]
  (str/trim (nth (parts s) 2 )))
 
(defn log-level
  "Takes a string representing a log line
   and returns its level in lower-case."
  [s]
  (str/lower-case (nth (parts s) 1 )))

(defn reformat
  "Takes a string representing a log line and formats it
   with the message first and the log level in parentheses."
  [s]
  (str (message s) " (" (log-level s) ")"))
` ``

Thanks a lot

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