如何获得正则表达式匹配在ClojureScript中的位置? [英] How can I get the positions of regex matches in ClojureScript?

查看:134
本文介绍了如何获得正则表达式匹配在ClojureScript中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clojure中,我可以使用像这样的解决方案: Compact Clojure代码用于正则表达式匹配及其在字符串中的位置,即创建重新匹配器并从中提取信息,但是重新匹配器似乎没有在ClojureScript中实现。在ClojureScript中完成同样的事情的好方法是什么?



编辑



我最后写了一个补充函数,以保存正则表达式的修饰符,因为它被吸收到 re-pos

 (defn regex-modifiers 
返回正则表达式的修饰符,串联为一个字符串
[re]
str(if(。-multiline re)m)
(if(。-ignoreCase re)i)))

(defn re-pos
一个向量的向量,每个子向量包含顺序:
匹配的位置,匹配的字符串,以及从匹配中提取的任何组

[re s]
(let [re(js / RegExp。(。-source re)(strg(regex-modifiers re)))
(loop [res []]
(if-let [m (.exec re s)]
(recur(conj res(vec(cons(。-index m)m))))
res)))


解决方案

您可以使用 RegExp 的US / docs / Web / JavaScript /参考/ Global_Objects / RegExp / exec> .exec 方法$ c> object。返回的匹配对象包含对应于字符串中匹配的索引的 index 属性。



当前clojurescript不支持使用 g 模式标志构造正则表达式文本(参见 CLJS-150 ),因此您需要使用 RegExp 构造函数。下面是一个clojurescript实现从链接页面的 re-pos 函数:

 (defn re-pos [re s] 
(let [re(js / RegExp。(.- source re)g)]
(loop [res {}]
(if-let [m(.exec re s)]
(recur(assoc res(。-index m)(first m)))
res)))

cljs.user> (re-pos\\w +快速棕狐狸)
{0The,4quick,10brown,16fox}
cljs.user> ; (re-pos[0-9] +3a1b2c1d)
{03,21,42,61}


In Clojure I could use something like this solution: Compact Clojure code for regular expression matches and their position in string, i.e., creating a re-matcher and extracted the information from that, but re-matcher doesn't appear to be implemented in ClojureScript. What would be a good way to accomplish the same thing in ClojureScript?

Edit:

I ended up writing a supplementary function in order to preserve the modifiers of the regex as it is absorbed into re-pos:

(defn regex-modifiers
  "Returns the modifiers of a regex, concatenated as a string."
  [re]
  (str (if (.-multiline re) "m")
       (if (.-ignoreCase re) "i")))

(defn re-pos
  "Returns a vector of vectors, each subvector containing in order:
   the position of the match, the matched string, and any groups
   extracted from the match."
  [re s]
  (let [re (js/RegExp. (.-source re) (str "g" (regex-modifiers re)))]
    (loop [res []]
      (if-let [m (.exec re s)]
        (recur (conj res (vec (cons (.-index m) m))))
        res))))

解决方案

You can use the .exec method of JS RegExp object. The returned match object contains an index property that corresponds to the index of the match in the string.

Currently clojurescript doesn't support constructing regex literals with the g mode flag (see CLJS-150), so you need to use the RegExp constructor. Here is a clojurescript implementation of the re-pos function from the linked page:

(defn re-pos [re s]
  (let [re (js/RegExp. (.-source re) "g")]
    (loop [res {}]
      (if-let [m (.exec re s)]
        (recur (assoc res (.-index m) (first m)))
        res))))

cljs.user> (re-pos "\\w+" "The quick brown fox")
{0 "The", 4 "quick", 10 "brown", 16 "fox"}
cljs.user> (re-pos "[0-9]+" "3a1b2c1d")
{0 "3", 2 "1", 4 "2", 6 "1"}

这篇关于如何获得正则表达式匹配在ClojureScript中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆