3

I'm trying to make my code more readable but still try have as much sense as possible, I would like to test a string vs a few regex patterns.

( re-find #"(?i)(^select .* from .*)|(^delete from me)" c))

And I would like to split this into 2 separate patterns but maybe use one test? is there anything that would test a set of patterns vs 1 string?

Thanks!

2
  • 3
    There's all kinds of constructs to do multiple tests. There isn't one "combine these regexps" function because there's too much variation. What do you want the result to be? For example, do you want results of each regexp? Each regexp that matches? The first regexp that matches? Commented Jan 24, 2013 at 19:17
  • Good point, missed it, updated my post. Only looking for a match/no match flag. Commented Jan 24, 2013 at 19:27

1 Answer 1

4
(def patterns [#"(?i)^select .* from .*" #"(?i)^delete from me"])

(when (some #(re-find % "your test string") patterns)
 ...)

clojure.core/some

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion! Just one some question: can I have an "else" or "default" for the when? i.e. can I somehow return false instead of the nil?
nil is a falsy value. You don't have to use when. if has an else form. (if test true-form else-form)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.