This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-23
Channels
- # beginners (81)
- # boot (1)
- # cider (1)
- # cljs-dev (15)
- # cljsrn (1)
- # clojure (26)
- # clojure-europe (9)
- # clojure-hamburg (2)
- # clojure-italy (6)
- # clojure-nl (6)
- # clojure-spec (10)
- # clojure-uk (33)
- # clojurescript (9)
- # clojurex (5)
- # cursive (14)
- # datomic (21)
- # devcards (2)
- # duct (72)
- # figwheel (1)
- # fulcro (6)
- # kaocha (3)
- # leiningen (5)
- # nrepl (10)
- # off-topic (65)
- # parinfer (12)
- # re-frame (68)
- # reagent (1)
- # reitit (14)
- # shadow-cljs (65)
- # spacemacs (6)
- # sql (4)
- # tools-deps (2)
- # yada (1)
This is probably off-topic even for #off-topic but,
I can specify a string less than 50 chars in length that is alphanumeric with this regex: ^\w{1,50}$
but how do I specify that the string should contain exactly one @
?
What you want is something like ^[^@]*@[^@]*$
so a start, than anything but not '@', then '@', then anything but '@', then the end.
Exactly. But I also want the constraint of max string length encoded in the same regex. Is that possible with regexes?
It is possible, but I don't know how to write it any shorter than a 50-way alternative ('or', using '|' as the usual syntax for separating alternatives).
One alternative for each of the possible 50 positions of the different character
You could write a small bit of code to help you generate such a regex. A question is: do you really need it to be a single regex, or can it instead be a small function instead?
Perhaps a simpler, less performant, approach is to break it into two pattern matches with the first doing the length check and on successful match, the second doing the [email protected] check.
Clojure Spec looks promising for this type of validation check: https://clojure.org/guides/spec
(s/def ::one-at-50max
(s/and #(re-matches #"^\w{1,50}$" %) #(= 1 (count (replace % #"[^@]" ""))))))
(s/conform "[email protected]")
I was writing a tiny subset of spec (no generators, no destructuring, no global semantics, only validation) where specs are pure data. I'm currently using a regex and a length bound to do this. Was just wondering if I could push the length check into the regex.
@jaihindh.reddy unless you are code golfing, just dont use regex
^ for speed?
Are you saying regex is faster than your code above?
There's faster libs out there though, because java's regex fsm isn't the most optimized.
you could definitely do this if you had direct access to the fsm. But I don't know that regex can express this easily.
I would think you’d want the regex that gklijs proposed, plus (<= (count s) 50)
I loved ToC
That's what I was thinking. \w{a}@\w{b}
where (< (+ a b) 50)
is not regular grammar. A Context free grammar can represent that of course.
Oops. My bad. For a general n
I guess
\w{0,0}@{0,49}|\w{0,1}@{0,48}|\w{0,2}@{0,47}|\w{0,3}@{0,46}|\w{0,4}@{0,45}|\w{0,5}@{0,44}|\w{0,6}@{0,43}|\w{0,7}@{0,42}|\w{0,8}@{0,41}|\w{0,9}@{0,40}|\w{0,10}@{0,39}|\w{0,11}@{0,38}|\w{0,12}@{0,37}|\w{0,13}@{0,36}|\w{0,14}@{0,35}|\w{0,15}@{0,34}|\w{0,16}@{0,33}|\w{0,17}@{0,32}|\w{0,18}@{0,31}|\w{0,19}@{0,30}|\w{0,20}@{0,29}|\w{0,21}@{0,28}|\w{0,22}@{0,27}|\w{0,23}@{0,26}|\w{0,24}@{0,25}|\w{0,25}@{0,24}|\w{0,26}@{0,23}|\w{0,27}@{0,22}|\w{0,28}@{0,21}|\w{0,29}@{0,20}|\w{0,30}@{0,19}|\w{0,31}@{0,18}|\w{0,32}@{0,17}|\w{0,33}@{0,16}|\w{0,34}@{0,15}|\w{0,35}@{0,14}|\w{0,36}@{0,13}|\w{0,37}@{0,12}|\w{0,38}@{0,11}|\w{0,39}@{0,10}|\w{0,40}@{0,9}|\w{0,41}@{0,8}|\w{0,42}@{0,7}|\w{0,43}@{0,6}|\w{0,44}@{0,5}|\w{0,45}@{0,4}|\w{0,46}@{0,3}|\w{0,47}@{0,2}|\w{0,48}@{0,1}|\w{0,49}@{0,0}
Oops. Off by one error
Came up with this:
(defn valid-VPA? [vpa]
(and (<= (count vpa) 50)
(= [\@] (filter #(not (Character/isLetterOrDigit %)) vpa))))
@(\*\*+\*)+\*(@\*+\*@[email protected])[email protected]
I am not sure what 'vpa' is referring to in that figure, but if by that you mean "a set of states in a nondeterministic finite automata, initially containing only the start state(s), and at each step updated to a new set as symbols of input are consumed", then yes, I think it can become empty, depending on how the state machine is constructed.
oh, see it now. oops.
nice catch. Shouldn't be empty
a VPA is like an email and is a unique handle you can transact money using UPI (Unified Payments Interface), something the Indian govt. came up with.
my only issue with your code is that (= [\@] (filter #(not (Character/isLetterOrDigit %)) vpa))
doesn't really read that well
True. I'm writing PHP though for work. And this is far better 🙂
Do you think, a team that adopted Elixir without anyone with significant experience in it, will do the same with ?
Someone else should take this question. I could give an answer but it would be far from complete.
but "PHP is bad clojure is better" doesn't help the fact that you still have a php codebase you probably cant just throw away
i am in no position to give advice, but first just try introducing it on a small project and see how it goes
We got some near real-time stream processing to do, and Riemann seems like just the thing.
Someone else should take this question. I could give an answer but it would be far from complete.