Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply "filter" based on regular expression?

Tags:

regex

clojure

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.

like image 667
A Friedrich Avatar asked Jan 07 '12 19:01

A Friedrich


People also ask

What can be matched using (*) in a regular expression?

You can repeat expressions with an asterisk or plus sign. A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

Can you use regex in Gmail filter?

Regex is not on the list of search features, and it was on (more or less, as Better message search functionality (i.e. Wildcard and partial word search)) the list of pre-canned feature requests, so the answer is "you cannot do this via the Gmail web UI" :-( There are no current Labs features which offer this.

How do I match a pattern in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .


1 Answers

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>")
like image 57
Rob Lachlan Avatar answered Nov 23 '22 23:11

Rob Lachlan