diff options
author | Paul Phillips <paulp@improving.org> | 2012-05-02 14:02:04 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-05-02 15:20:25 -0700 |
commit | 1475df9bedc03417708f20d94b5e3db5c80f3036 (patch) | |
tree | 6dc7e6fee60f17ce379766def8ec5b9f1918b3d5 /test | |
parent | 4b8c54cd9e52dbacc239d05c8149d7f249bbebab (diff) | |
download | scala-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')
-rw-r--r-- | test/files/run/si5045.check | 6 | ||||
-rw-r--r-- | test/files/run/si5045.scala | 46 |
2 files changed, 52 insertions, 0 deletions
diff --git a/test/files/run/si5045.check b/test/files/run/si5045.check new file mode 100644 index 0000000000..7e9c1961b7 --- /dev/null +++ b/test/files/run/si5045.check @@ -0,0 +1,6 @@ + extract an exact match 2011-07-15 2011-07-15 + extract from middle of string 2011-07-15 2011-07-15 + extract from middle of string (P2) 2011-07-15 2011-07-15 + extract from middle of string (P3) 2011-07-15 2011-07-15 + copyright example has date Copyright 2011 Copyright 2011 + copyright example missing date No copyright No copyright 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") + } +} |