A Friedrich
A Friedrich

Reputation: 603

How to apply "filter" based on regular expression?

Is it possible to apply filter based on a regular expression? What I had in mind was something like

(filter #"<+\p{Alnum}+>" ["abc" "<def>"])

to return

=> ["<def>"]

Thanks in advance for hints.

Upvotes: 7

Views: 7202

Answers (1)

Rob Lachlan
Rob Lachlan

Reputation: 14469

Put your regex inside an anonymous function that tests matching to your regex. The general form would be:

(filter #(re-matches REGEX %) SEQUENCE)

Where REGEX is the regex that you're interested in, and SEQUENCE is the sequence that you're interested in. Trying your example,

user> (filter #(re-matches #"<+\p{Alnum}+>" %) ["abc" "<def>"])

("<def>")

Upvotes: 9

Related Questions