summaryrefslogtreecommitdiff
path: root/test/files/run/si5045.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2012-05-02 14:02:04 -0700
committerPaul Phillips <paulp@improving.org>2012-05-02 15:20:25 -0700
commit1475df9bedc03417708f20d94b5e3db5c80f3036 (patch)
tree6dc7e6fee60f17ce379766def8ec5b9f1918b3d5 /test/files/run/si5045.scala
parent4b8c54cd9e52dbacc239d05c8149d7f249bbebab (diff)
downloadscala-1475df9bedc03417708f20d94b5e3db5c80f3036.tar.gz
scala-1475df9bedc03417708f20d94b5e3db5c80f3036.tar.bz2
scala-1475df9bedc03417708f20d94b5e3db5c80f3036.zip
Unanchored regex extractors.
This patch is really by Lanny Ripple <lanny@spotinfluence.com>, but I reworked it because I didn't want to put any more methods onto String. Instead, there is a method on Regex which removes the anchoring quality. """\d\d'"".r.unanchored
Diffstat (limited to 'test/files/run/si5045.scala')
-rw-r--r--test/files/run/si5045.scala46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/files/run/si5045.scala b/test/files/run/si5045.scala
new file mode 100644
index 0000000000..e198b101f3
--- /dev/null
+++ b/test/files/run/si5045.scala
@@ -0,0 +1,46 @@
+object Test extends App {
+
+ import scala.util.matching.{ Regex, UnanchoredRegex }
+
+ val dateP1 = """(\d\d\d\d)-(\d\d)-(\d\d)""".r.unanchored
+ val dateP2 = """(\d\d\d\d)-(\d\d)-(\d\d)""" r ("year", "month", "day") unanchored
+ val dateP3 = new Regex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day") with UnanchoredRegex
+
+ val yearStr = "2011"
+ val dateStr = List(yearStr,"07","15").mkString("-")
+
+ def test(msg: String)(strs: Seq[String]): Unit = println("%40s %s".format(msg, strs mkString " "))
+
+ test("extract an exact match") {
+ val dateP1(y,m,d) = dateStr
+ Seq(List(y,m,d).mkString("-"), dateStr)
+ }
+
+ test("extract from middle of string") {
+ val dateP1(y,m,d) = "Tested on "+dateStr+"."
+ Seq(List(y,m,d).mkString("-"), dateStr)
+ }
+
+ test("extract from middle of string (P2)") {
+ val dateP2(y,m,d) = "Tested on "+dateStr+"."
+ Seq(List(y,m,d).mkString("-"), dateStr)
+ }
+
+ test("extract from middle of string (P3)") {
+ val dateP2(y,m,d) = "Tested on "+dateStr+"."
+ Seq(List(y,m,d).mkString("-"), dateStr)
+ }
+
+ def copyright(in: String): String = in match {
+ case dateP1(year, month, day) => "Copyright "+year
+ case _ => "No copyright"
+ }
+
+ test("copyright example has date") {
+ Seq(copyright("Date of this document: "+dateStr), "Copyright "+yearStr)
+ }
+
+ test("copyright example missing date") {
+ Seq(copyright("Date of this document: unknown"), "No copyright")
+ }
+}