-
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: add string match for easily handling sub-string. [merged, looking for feedback] #113
Comments
Nice, that is pretty interesting. With some indentation the snippet becomes (str-match "a1c5b"
(("a" b "c")
(parse-integer b))
(("a" x "c" y "b")
(print (parse-integer x))
(print (parse-integer y))
(list (parse-integer x) (parse-integer y)))
(t (print "aa"))) so by using (str-match "a1c5b"
(("a" b "c")
(parse-integer b))
(("a" x "c" y "b")
(print (parse-integer x))
(print (parse-integer y))
(list (parse-integer x) (parse-integer y)))
(t (print "aa"))) Would you not use the Trivia library for pattern matching? It probably does this, and more. What are users going to ask for pattern matching features after we introduce this one?
what are your favourite examples? (and yes "string-match" might be better) |
Nice catch!
Gonna check it now. |
I checked the (trivia:match '(1 2 3)
((list* 1 x _)
x)
((list* _ x)
x)) ;; => 2 but I have an issue when I run the string pattern. I am not sure because I am using sbcl or not (maybe because this?) beside, I can match the whole string like (trivia:match "a1c5b" ("a1c5b" 1))
;; or
(trivia:match "ab" ((vector #\a #\b) 1)) but not these: (trivia:match "a1c5b" ((string "a1c" "5b") 1)) so look like I can only binding char rather than the sub-string like my purposal |
Let's use and try this macro. I'm interested in everybody's feedback. A stupid test: I match like your example, but I don't use the matching variable, so I get style warnings:
Would it be possible to avoid the warnings? Using a |
Yes, I just try on my side. Will give PR soon. |
Gave the PR #114 |
I tried this more on an AOC problem (day 19), and OMG this match macro felt so powerful. Easier and faster than searching for the right regexp. |
Other quick test: (str::match "123 hello 456"
(("\\d+" s "\\d+")
s)
(t "nothing"))
;; =>" hello 45" I didn't expect to see "45". The first number regex was correctly matched, not the second? (str::match "123 hello 456"
(("\\d+" s "\\d*")
s)
(t "nothing"))
;; " hello 456" here I didn't expect "456". |
@vindarel Just figure out fixing this issue need to write the un-greedy regex. I just fix it in the latest commit. Good catch! |
The PR #114 is merged, I am not sure if we keep this idea issue open or not for future potential changes. I left this decision to repo owner. |
I was thinking if the
cl-str
can "pattern match" the string like some other languages' match case.So I write my own version (like example below), what do you guys think? Is that fit the cl-str's purpose? I checked the doc and there is a
string-case
. Should I change the name of the macro? Thanks!The text was updated successfully, but these errors were encountered: