diff options
author | Daniel C. Sobral <dcsobral@gmail.com> | 2012-01-25 13:30:40 -0200 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-03-23 06:57:00 -0700 |
commit | e3dec9f006ac2631281fb936c4ca206daa8fda5d (patch) | |
tree | 4c2bb8efc68199fde4d51a58a20f5ded595efeb5 /src/library/scala/util/matching/Regex.scala | |
parent | 8f42361d71d11e9522052dcb5d9be020df7e5cc5 (diff) | |
download | scala-e3dec9f006ac2631281fb936c4ca206daa8fda5d.tar.gz scala-e3dec9f006ac2631281fb936c4ca206daa8fda5d.tar.bz2 scala-e3dec9f006ac2631281fb936c4ca206daa8fda5d.zip |
Regex improvements
This adds findAllMatchIn to Regex to mirror other similar methods.
It also overloads StringLike's "r", adding a version that accepts
group names.
It includes test cases for both methods.
Closes SI-2460.
Diffstat (limited to 'src/library/scala/util/matching/Regex.scala')
-rw-r--r-- | src/library/scala/util/matching/Regex.scala | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala index ca97515e23..2debd247b8 100644 --- a/src/library/scala/util/matching/Regex.scala +++ b/src/library/scala/util/matching/Regex.scala @@ -180,7 +180,7 @@ class Regex(regex: String, groupNames: String*) extends Serializable { None } - /** Return all matches of this regexp in given character sequence as a [[scala.util.mathcing.Regex.MatchIterator]], + /** Return all matches of this regexp in given character sequence as a [[scala.util.matching.Regex.MatchIterator]], * which is a special [[scala.collection.Iterator]] that returns the * matched strings, but can also be converted into a normal iterator * that returns objects of type [[scala.util.matching.Regex.Match]] @@ -193,6 +193,25 @@ class Regex(regex: String, groupNames: String*) extends Serializable { */ def findAllIn(source: java.lang.CharSequence) = new Regex.MatchIterator(source, this, groupNames) + + /** Return all matches of this regexp in given character sequence as a + * [[scala.collection.Iterator]] of [[scala.util.matching.Regex.Match]. + * + * @param source The text to match against. + * @return A [[scala.collection.Iterator]] of [[scala.util.matching.Regex.Match]] for all matches. + * @example {{{for (words <- """\w+""".r findAllMatchIn "A simple example.") yield words.start}}} + */ + def findAllMatchIn(source: java.lang.CharSequence): Iterator[Match] = { + val matchIterator = findAllIn(source) + new Iterator[Match] { + def hasNext = matchIterator.hasNext + def next: Match = { + matchIterator.next; + new Match(matchIterator.source, matchIterator.matcher, matchIterator.groupNames).force + } + } + } + /** Return optionally first matching string of this regexp in given character sequence, * or None if it does not exist. * @@ -505,7 +524,7 @@ object Regex { class MatchIterator(val source: java.lang.CharSequence, val regex: Regex, val groupNames: Seq[String]) extends AbstractIterator[String] with Iterator[String] with MatchData { self => - protected val matcher = regex.pattern.matcher(source) + protected[Regex] val matcher = regex.pattern.matcher(source) private var nextSeen = false /** Is there another match? */ |