magnars/s.el

enhancement: `s-extract-match` to get a (sub)match from a string

Closed this issue · 2 comments

I find myself, more often than I would like, performing single element extraction based on a regex; the implementation is probably trivially obvious, but proposed signature:

(s-extract-match regexp s &optional subexp-depth)

Returns the Nth subexpression match, defaulting to "0" or full match, from running regexp vs string.

(s-match "\\(1\\) 2 3 \\(3.3\\) 4" "1 2 3 3.3 4")
=> ("1 2 3 3.3 4" "1" "3.3")

so...

(nth 1 (s-match "\\(1\\) 2 3 \\(3.3\\) 4" "1 2 3 3.3 4"))
=> "1"
(nth 2 (s-match "\\(1\\) 2 3 \\(3.3\\) 4" "1 2 3 3.3 4"))
=> "3.3"

or...

(cadr (s-match "\\(1\\) 2 3 \\(3.3\\) 4" "1 2 3 3.3 4"))
=> "1"
(caddr (s-match "\\(1\\) 2 3 \\(3.3\\) 4" "1 2 3 3.3 4"))
=> "3.3"

Closing as more verbose form than using ca[ddd]r or nth n.