-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add r method to StringContext (for regexes) #7496
Comments
Imported From: https://issues.scala-lang.org/browse/SI-7496?orig=1 |
@qerub said: |
@soc said (edited on May 18, 2013 2:27:49 PM UTC): I think there are already a few implementations of that, including one in which you can name the capture group inline. Would be interesting to check and compare what those implementations do and figure out what's a nice subset to include. |
@qerub said: I think I found an implementation of what you're mentioning: http://dcsobral.blogspot.com/2012/01/string-interpolation-on-scala-210.html Yes, I agree. Let's proceed by collecting all relevant ideas. |
@qerub said: |
@dcsobral said: Personally, I think that style is error prone, since the order in which things get assigned is not related to their position, but to the number of capturing groups. Add a capture group by mistake, and the match will start failing. Of these two, I do prefer my own version (as described in my blog post already mentioned), and I do like What I'd really rather do is go with macros and use named patterns. Java 7 supports |
@qerub said: I like your idea with named capture groups but I don't know how to implement it; it requires a code transform that's non-local to unapplySeq. Any hints? |
@adriaanm said: |
@jroper said: |
@SethTisue said: |
@soc said: |
Scala currently has a method
StringLike#r
for compiling strings to regexes.Now that Scala has string interpolation and macros I think we can do even better with a
r
method onStringContext
. Here is why:Escaping gets simpler (before:
"""\d+""".r
, after:r"\d+"
)Parameterization gets simpler (before:
("foo"+Pattern.escape(bar)).r
, after:r"foo$bar"
)Regexes can be syntax-checked at compile-time
Pattern matching against regexes gets simpler (before:
val re = """\d+(.*)\d+""".r; … case
re\(x\) => x
, after:case r"\d+$x\d+" => x
)I have a proof of concept implementation available at https://github.com/qerub/scala-regex-stringcontext and can make a patch for Scala proper if people like this idea.
The text was updated successfully, but these errors were encountered: