summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2013-08-14 13:29:10 -0700
committerAdriaan Moors <adriaan.moors@typesafe.com>2013-08-14 13:29:10 -0700
commit5bdb828a2ef3fa874c2c19e8415d3d670c022ae1 (patch)
tree13618fa2427a03feb4237baed184dd5f17a6a9df /src
parent275194a62989115e065ff69c41942f113db8b8f7 (diff)
parent93e9623f5babee93c50eb1ce82e443d6771b693d (diff)
downloadscala-5bdb828a2ef3fa874c2c19e8415d3d670c022ae1.tar.gz
scala-5bdb828a2ef3fa874c2c19e8415d3d670c022ae1.tar.bz2
scala-5bdb828a2ef3fa874c2c19e8415d3d670c022ae1.zip
Merge pull request #2819 from som-snytt/issue/regextract-char
SI-7737 Regex matches Char
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/util/matching/Regex.scala38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala
index 8eac0a2520..439b30e714 100644
--- a/src/library/scala/util/matching/Regex.scala
+++ b/src/library/scala/util/matching/Regex.scala
@@ -194,6 +194,44 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
else None
}
+ /** Tries to match the String representation of a [[scala.Char]].
+ * If the match succeeds, the result is the first matching
+ * group if any groups are defined, or an empty Sequence otherwise.
+ *
+ * For example:
+ *
+ * {{{
+ * val cat = "cat"
+ * // the case must consume the group to match
+ * val r = """(\p{Lower})""".r
+ * cat(0) match { case r(x) => true }
+ * cat(0) match { case r(_) => true }
+ * cat(0) match { case r(_*) => true }
+ * cat(0) match { case r() => true } // no match
+ *
+ * // there is no group to extract
+ * val r = """\p{Lower}""".r
+ * cat(0) match { case r(x) => true } // no match
+ * cat(0) match { case r(_) => true } // no match
+ * cat(0) match { case r(_*) => true } // matches
+ * cat(0) match { case r() => true } // matches
+ *
+ * // even if there are multiple groups, only one is returned
+ * val r = """((.))""".r
+ * cat(0) match { case r(_) => true } // matches
+ * cat(0) match { case r(_,_) => true } // no match
+ * }}}
+ *
+ * @param c The Char to match
+ * @return The match
+ */
+ def unapplySeq(c: Char): Option[Seq[Char]] = {
+ val m = pattern matcher c.toString
+ if (runMatcher(m)) {
+ if (m.groupCount > 0) Some(m group 1) else Some(Nil)
+ } else None
+ }
+
/** Tries to match on a [[scala.util.matching.Regex.Match]].
* A previously failed match results in None.
* If a successful match was made against the current pattern, then that result is used.