Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure Parse String

Tags:

regex

clojure

I have the following string

layout: default
title: Envy Labs

What i am trying to do is create map from it

layout->default 
title->"envy labs"

Is this possible to do using sequence functions or do i have to loop through each line?

Trying to get a regex to work with and failing using.


(apply hash-map (re-split #": " meta-info))
like image 419
Hamza Yerlikaya Avatar asked Feb 28 '23 07:02

Hamza Yerlikaya


2 Answers

user> (let [x "layout: default\ntitle: Envy Labs"]
        (reduce (fn [h [_ k v]] (assoc h k v))
                {}
                (re-seq #"([^:]+): (.+)(\n|$)" x)))
{"title" "Envy Labs", "layout" "default"}
like image 53
Brian Carper Avatar answered Mar 07 '23 15:03

Brian Carper


The _ is a variable name used to indicate that you don't care about the value of the variable (in this case, the whole matched string).

like image 21
Jim Downing Avatar answered Mar 07 '23 16:03

Jim Downing