Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
johannes karoff
@cornerman
@nrinaudo thanks for the response! how would i go about doing that? :)
Nicolas Rinaudo
@nrinaudo
sorry, missed your message.
So the principle is fairly straightforward - each evaluation yields an Either[Error, A], so you could write something like:
for {
  i <- str.evalRegex[Int](regex1)
  b <- str.evalRegex[Boolean](regex2)
} yield (i, b)
where regex1 and regex2 are the two regular expressions you with to compose and strthe string you wish to apply them to
@cornerman let me know if this isn't clear enough. If you were to give me a concrete use case, I could probably wip up some quick code to show you
johannes karoff
@cornerman
@nrinaudo ah got you. thats what you meant with decoding mechanism :)
my use-case is parsing a url-like structure. so I have a website, where i need to interpret the url (or more specifically the hash part of the url). so i have something similar to query parameters or key-value pairs like something=false&list=1,2,3&nothing=. so depending on the key, i need to interpret the rhs differently.
i was using fastparse previously to have a grammer for the options in the url. so i could write a parser for each option and then combine them in one grammar. that was nice. but is very slow on startup in scala.js.
johannes karoff
@cornerman
what i am doing now: i take the url and first match Map[String ->String] with a simple regex for k=v. and then i parse each entry in the map. so in the example, i would have then a regex for something, list and nothing.
so, it works now :)
johannes karoff
@cornerman
on another note: when i was using a star in my regex, e.g. (\w+)(:\d+)*. and then compiled a Regex[(String,String)] or Regex[(String,Seq[String])], I always got an error about an "empty group". I then went for (\w+)(:\d+)*? and used Regex[(String, Option[String])]. what can i do about this?
Nicolas Rinaudo
@nrinaudo
@cornerman Not sure about your use case for URI parsing, I would probably use a dedicated library for that as URI parameter parsing isn't as trivial as it might seem at first glance
johannes karoff
@cornerman
@nrinaudo in general i agree but it is not really a url. we have a SPA app running in the browser, which writes its current options to the hash-part of the url. the options are key-value pairs and similar to an url with regards to being a sequence of key-value pairs separated by & and =. so a dedicated uri parsing library would not really fit.
Nicolas Rinaudo
@nrinaudo
and as for your example, I'll need to see your code, because I don't think I understand what you're saying:
"abc:123".evalRegex[(String, String)](rx"(\w+)(:\d+)*").next
res1: kantan.regex.DecodeResult[(String, String)] = Right((abc,:123))
johannes karoff
@cornerman
yes, your example works, but if you have no match for (:\d+)*, it does not work:
"abc".evalRegex[(String, String)](rx"(\w+)(:\d+)*").next
res1: kantan.regex.DecodeResult[(String, String)] = Left(EmptyGroup: an empty group was found)
Nicolas Rinaudo
@nrinaudo
well yeah - the second group is not matched
so you get a match with a single group, which is what this is telling you
you have basically two choices there:
  • make the second group optional
  • make slightly more complicated groups (this would also allow you to ignore the :, which I'm assuming is desirable but I might be wrong)
johannes karoff
@cornerman
but semantically an empty string is a match for something* in a regex, isn't it?
Nicolas Rinaudo
@nrinaudo
you don't have an empty string. You have nothing
wait, what do you think the : means in your regex?
is that a separator, or did you think it means "non-capturing group"?
johannes karoff
@cornerman
no, just a separator
Nicolas Rinaudo
@nrinaudo
right
johannes karoff
@cornerman
i was under the impression, i could have a Regex[(String, Option[String])] from a *. just like from a ?. because both can yield an empty result
Nicolas Rinaudo
@nrinaudo
so then:
"abc:".evalRegex[(String, String)](rx"(\w+)(:\d*)").next
johannes karoff
@cornerman
but that is not what I want to match
Nicolas Rinaudo
@nrinaudo
but let's take it from the top. What are you precisely trying to do?
you can absolutely get an option from a * (I think), it's just that your regex requires : which your input doesn't contain
johannes karoff
@cornerman
oh, actually i really can!
sorry! it just works, i just made a mistake in my code :/
Nicolas Rinaudo
@nrinaudo
I understand what the first part of the regex is - you want a word as a key. The second part is more obscure. Do you require a :? If not but if it's present, can the \d sequence be empty?
oh, ok then
glad I could help, even if I'm not sure how!
johannes karoff
@cornerman
thank you for your time!
Stefan Endrullis
@xylo

In case that someone uses Intellij IDEA: I just added support for kantan.regex to my plugin called Custom Postfix Templates. It allows you to easily expand

"abc:".evalRegex

to

"abc:".evalRegex(rx"")

by automatically adding

import kantan.regex._
import kantan.regex.implicits._
import kantan.regex.generic._

to your imports.

Moreover,

"xyz".rx

is expanded to

rx"xyz"

also by adding the import.

Please let me know if I can improve these postfix templates.