release | |||
dev |
A micro-library for Clojure that provides first class support for accessing the values of named-capturing groups in regular expressions. It has no dependencies, other than on Clojure and the JVM versions it supports, and is only around 100 lines of code.
rencg
work on older JVMs that don't have the .namedGroups()
API?Yes. On older (pre v20) JVMs, rencg
falls back on manually parsing regexes to determine the named capturing groups.
Because that library monkey patches core Clojure, which may break other code.
rencg
is available as a Maven artifact from Clojars.
$ clj -Sdeps '{:deps {com.github.pmonks/rencg {:mvn/version "RELEASE"}}}'
$ lein try com.github.pmonks/rencg
$ deps-try com.github.pmonks/rencg
(require '[rencg.api :as rencg])
;; re-matches-ncg - for when you want to match the entire input
(rencg/re-matches-ncg #"(?<foo>foo)" "bar")
;=> nil
(rencg/re-matches-ncg #"(?<foo>foo)" "foo")
;=> {:start 0, :end 3, "foo" "foo"}
(rencg/re-matches-ncg #"(?<foo>foo)+" "foofoo")
;=> {:start 0, :end 6, "foo" "foo"}
; Note: Java named capturing groups only capture a single value from the input, even if the
; group is present multiple times. Also, the start and end indexes are for the entire match,
; not where the named capturing groups are found (obviously, since there may be many named
; capturing groups all of which have different start and end indexes).
(rencg/re-matches-ncg #"((?<foo>foo)|(?<bar>bar))+" "foobarfoobarfoobarfoobar")
;=> {:start 0, :end 24, "foo" "foo", "bar" "bar"}
; This last example also shows the value of using named capturing groups instead of numbered
; capturing groups (the latter being brittle, since non-named groups conflate grouping and
; capture)
;; re-seq-ncg - for when you want all matches of a named capturing group that exist within
;; the input
(rencg/re-seq-ncg #"((?<foo>foo)|(?<bar>bar))" "foobarfoobarfoobarfoobar")
;=> ({:start 0, :end 3, "foo" "foo"}
; {:start 3, :end 6, "bar" "bar"}
; {:start 6, :end 9, "foo" "foo"}
; {:start 9, :end 12, "bar" "bar"}
; {:start 12, :end 15, "foo" "foo"}
; {:start 15, :end 18, "bar" "bar"}
; {:start 18, :end 21, "foo" "foo"}
; {:start 21, :end 24, "bar" "bar"})
;; re-find-ncg - for when you want to extract something specific from the input, using
;; standard Clojure map lookups
(get (rencg/re-find-ncg #"(?i)(?<foo>foo)" "THIS IS SOME TEXT WITH FOO IN IT") "foo")
;=> "FOO"
API documentation is available here, or here on cljdoc, and the unit tests are also worth perusing to see worked examples.
This project uses the git-flow branching strategy, and the permanent branches are called release
and dev
. Any changes to the release
branch are considered a release and auto-deployed (JARs to Clojars, API docs to GitHub Pages, etc.).
For this reason, all development must occur either in branch dev
, or (preferably) in temporary branches off of dev
. All PRs from forked repos must also be submitted against dev
; the release
branch is only updated from dev
via PRs created by the core development team. All other changes submitted to release
will be rejected.
rencg
uses tools.build
. You can get a list of available tasks by running:
clojure -A:deps -T:build help/doc
Of particular interest are:
clojure -T:build test
- run the unit testsclojure -T:build lint
- run the linters (clj-kondo and eastwood)clojure -T:build ci
- run the full CI suite (check for outdated dependencies, run the unit tests, run the linters)clojure -T:build install
- build the JAR and install it locally (e.g. so you can test it with downstream code)Please note that the release
and deploy
tasks are restricted to the core development team (and will not function if you run them yourself).
Copyright © 2023 Peter Monks
Distributed under the Apache License, Version 2.0.
SPDX-License-Identifier: Apache-2.0
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close