A Regular Expression composition Library
REL is a Scala library for people dealing with complex, modular regular expressions. It defines a DSL with most of the operators you already know and love. This allows you to isolate portions of your regex for easier testing and reuse.
Consider the following YYYY-MM-DD date regex: ^(?:19|20)\d\d([- /.])(?:0[1-9]|1[012])\1(?:0[1-9]|[12]\d|3[01])$
It is a bit more readable and reusable expressed like this:
import fr.splayce.rel._
import Implicits._
val sep = "[- /.]" \ "sep" // group named "sep"
val year = ("19" | "20") ~ """\d\d""" // ~ is concatenation
val month = "0[1-9]" | "1[012]"
val day = "0[1-9]" | "[12]\\d" | "3[01]"
val dateYMD = ^ ~ year ~ sep ~ month ~ !sep ~ day ~ $
val dateMDY = ^ ~ month ~ sep ~ day ~ !sep ~ year ~ $
These values are RE
objects (also named terms or trees/subtrees), which can be converted to scala.util.matching.Regex
instances either implicitly (by importing rel.Implicits._
) or explicitly (via the .r
method).
The embedded Date regexes and extractors will give you more complete examples, matching several date formats at once with little prior knowledge.
Copyright © 2012 Imaginatio SAS
REL is released under the Creative Commons BY-NC-SA 4.0 International License.
REL was developed at Imaginatio for project Splayce by:
Contributors:
TrackString
algorithm