开发者

How to extract the last value before last : character but after the last comma?

I'm using Clojure to parse and analyze XML files.

Here is a sample:

BSS:17,NSVC:1
BSS:17,NSVC:4
BSS开发者_如何转开发:17,NSVC:5
BSS:17,BTSM:0,BTS:3
BSS:17,BTSM:0,BTS:4
BSS:17,BTSM:0,BTS:5
BSS:17,BTSM:1,BTS:0
BSS:17,BTSM:1,BTS:1
BSS:17,BTSM:1,BTS:2
BSS:17,BTSM:1,BTS:3

I'm interested in that last value (a value after the last comma but before the last : , NSVS and BTS in my case), digits after them don't matter.

How to extract that last value in the previous strings?


You can use this function to process the individual lines:

(defn lastval [s]
  (second (re-find #",([^,:]+):\d*$" s)))
                 ;   ^ the comma preceding the interesting section
                 ;    ^ the part in parens will be captured as a group
                 ;     ^ character class meaning "anything except , or :"
                 ;            ^ the colon after the interesting section
                 ;             ^ any number of digits after the colon
                 ;                ^ end of string
          ; ^ returns a vector of [part-that-matches, first-group];
          ;   we're interested in the latter, hence second

NB. this returns nil if the regex doesn't match.

E.g.:

user> (lastval "BSS:17,BTSM:0,BTS:3")
"BTS"

If you later want to extract all the information in easy-to-work-with bits, you can use

(defn parse [s]
  (map (juxt second #(nth % 2)) (re-seq #"(?:^|,)([^,:]+):(\d+)" s)))

E.g.

user> (parse "BSS:17,BTS:0,BTS:3")
(["BSS" "17"] ["BTS" "0"] ["BTS" "3"])


Can you use lastIndexOf to find the last comma?


Does this work for you?


(def tmp (str "BSS:17,NSVC:1\n
BSS:17,NSVC:4\n
BSS:17,NSVC:5\n
BSS:17,BTSM:0,BTS:3\n
BSS:17,BTSM:0,BTS:4\n
BSS:17,BTSM:0,BTS:5\n
BSS:17,BTSM:1,BTS:0\n
BSS:17,BTSM:1,BTS:1\n
BSS:17,BTSM:1,BTS:2\n
BSS:17,BTSM:1,BTS:3\n"))


(defn split [s sep]
  (->> (.split s sep)
       seq
       (filter #(not (empty? %)))))

(reduce (fn[h v]
          (conj h (last v)))

          [] (map #(split % ",")
                  (split tmp "\n")))

I am assuming there is some sort of delimeter between the lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜