propensive / kaleidoscope翻译 / 编辑

最近提交:3天前
创建时间:2018.02.13

语言构成

Scala92.8%
Makefile7.2%

README

Latest version Maven Central

Kaleidoscope

Join the chat at https://gitter.im/propensive/kaleidoscope

Availability

An early release of Kaleidoscope is available on Maven Central, and can be included in an sbt project by including,

"com.propensive" %% "kaleidoscope" % "0.1.0"

in your build.sbt file.

Usage

To use Kaleidoscope, first import its package,

import kaleidoscope._

and you can then use a Kaleidoscope regular expression—a string prefixed with the letter r—anywhere you can use a pattern in Scala. For example,

path match {
  case r"/images/.*" => println("image")
  case r"/styles/.*" => println("stylesheet")
  case _ => println("something else")
}

or,

email match {
  case r"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$$" => Some(email)
  case _ => None
}

Such patterns with either match or not, however should they match, it is possible to extract parts of the matched string using capturing groups. The pattern syntax is exactly as described in the Java Standard Library, with the exception that a capturing group (enclosed within ( and )) may be bound to an identifier by prefixing the group with an @, and the identifier to extract to, which in standard Scala syntax, is written either as $identifier or ${identifier}.

Here is an example:

path match {
  case r"/images/${img}@(.*)" => Image(img)
  case r"/styles/$styles@(.*)" => Stylesheet(styles)
}

or as an extractor on a val, like so,

val r"^[a-z0-9._%+-]+@$domain@([a-z0-9.-]+\.$tld@([a-z]{2,6})$$" = "test@example.com"
> domain: String = "example.com"
> tld: String = "com"

In addition, regular expressions will be checked at compile-time, and any issues will be reported then.

Escaping

Note that inside an extractor pattern string, whether it is single- (r"...") or triple-quoted (r"""..."""), special characters, notably \, do not need to be escaped, with the exception of $ which should be written as $$. It is still necessary, however, to follow the regular expression escaping rules, for example, an extractor matching a single opening parenthesis would be written as r"\(" or r"""\(""".

Limitations

Kaleidoscope currently has no support for optional or repeated capturing groups, however this will probably be added very soon.

Disclaimer

This is very experimental software, and should not be considered production-ready.

Website

There is currently no website for Kaleidoscope.

License

Kaleidoscope is made available under the Apache 2.0 license.

讨论区

说说你的看法