- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Regex?
Sat, 2009-01-31, 01:04
Is there any documentation on using regular expressions in Scala? In particular, I'd like to
understand the String#r stunt that's going on.
scala> "(? "(? "(? "o".r.findAllIn("foo").groupCount
res6: Int = 0
scala> """o""".r.findAllIn("foo").groupCount
res7: Int = 0
~~ Robert Fischer.
Grails Trainining http://www.smokejumperit.com/grails_training.html
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html
implicit def stringWrapper(x: String) = new runtime.RichString(x)
and in scala.runtime.RichString you'll find the "r" method.
/** You can follow a string with `.r', turning
* it into a Regex. E.g.
*
* """A\w*""".r is the regular expression for identifiers starting with `A'.
*/
def r: Regex = new Regex(self)
which converts the RichString to a Regex object. The rest happens in scala.util.matching.Regex which essentially delegates to java.util.regex.{Pattern, Matcher} classes.
hope this helps,
alex
On Fri, Jan 30, 2009 at 4:03 PM, Robert Fischer <robert.fischer@smokejumperit.com> wrote: