1. 程式人生 > >scala之正則表示式(二)內部匹配函式

scala之正則表示式(二)內部匹配函式

1、scala解析正則表示式步驟

以下面表達為例:

val dateP1 = new scala.util.matching.Regex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day")
圖1:提取匹配資訊 在上面的基礎上我們在來做一些事情,如下:
val terday = "terday is 2016-8-2"
dateP1 findAllIn terday toList


2、findAllIn函式

功能:把全部的匹配值找出

def findAllIn(source: java.lang.CharSequence) = new 
Regex.MatchIterator(source, this, groupNames)

3、findFirstIn函式

功能:把第一個的匹配值找出

def findFirstIn(source: java.lang.CharSequence): Option[String] = {
  val m = pattern.matcher(source)
  if (m.find) Some(m.group) else None
}

4、findAllMatchIn函式

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 } } }

5、findPrefixOf函式

功能:找出字首為正則表示式

def 
findPrefixOf(source: java.lang.CharSequence): Option[String] = { val m = pattern.matcher(source) if (m.lookingAt) Some(m.group) else None }

6、findPrefixMatchOf函式

def findPrefixMatchOf(source: java.lang.CharSequence): Option[Match] = {
  val m = pattern.matcher(source)
  if (m.lookingAt) Some(new Match(source, m, groupNames)) else None
}

比較

1、findAllIn和findAllMatchIn的區別

也就是返回型別Option[Match]和Option[String]的區別

(1)型別上的區別


可以說findAllMatchIn比findAllIn更加抽象,功能更加多樣,findAllMatchIn可以在返回型別Match中可以增加一些操作

比如從那個位置開始,那個位置結束的。並可以提取他們的值。

以下是 返回Option[Match]可以有的操作函式:

traitMatchData {

  /** The source from where the match originated */
val source: java.lang.CharSequence

  /** The names of the groups, or some empty sequence if one defined */
val groupNames: Seq[String]

  /** The number of subgroups in the pattern (not all of these need to match!) */
def groupCount: Int
/** The index of the first matched character, or -1 if nothing was matched */
def start: Int
/** The index of the first matched character in group `i`,
   *  or -1 if nothing was matched for that group */
def start(i: Int): Int
/** The index of the last matched character, or -1 if nothing was matched */
def end: Int
/** The index following the last matched character in group `i`,
   *  or -1 if nothing was matched for that group */
def end(i: Int): Int
/** The matched string, or `null` if nothing was matched */
def matched: String =
    if (start >= 0) source.subSequence(start, end).toString
    else null
/** The matched string in group `i`,
   *  or `null` if nothing was matched */
def group(i: Int): String =
    if (start(i) >= 0) source.subSequence(start(i), end(i)).toString
    else null
/** All matched subgroups, i.e. not including group(0) */
def subgroups: List[String] = (1 to groupCount).toList map group

  /** The char sequence before first character of match,
   *  or `null` if nothing was matched */
def before: java.lang.CharSequence =
    if (start >= 0) source.subSequence(0, start)
    else null
/** The char sequence before first character of match in group `i`,
   *  or `null` if nothing was matched for that group  */
def before(i: Int): java.lang.CharSequence =
    if (start(i) >= 0) source.subSequence(0, start(i))
    else null
/** Returns char sequence after last character of match,
   *  or `null` if nothing was matched */
def after: java.lang.CharSequence =
    if (end >= 0) source.subSequence(end, source.length)
    else null
/** The char sequence after last character of match in group `i`,
   *  or `null` if nothing was matched for that group  */
def after(i: Int): java.lang.CharSequence =
    if (end(i) >= 0) source.subSequence(end(i), source.length)
    else null
  private lazy val nameToIndex: Map[String, Int] = Map[String, Int]() ++ ("" :: groupNames.toList).zipWithIndex

  /** Returns the group with given name
   *
   *  @param id The group name
   *  @return   The requested group
   *  @throws   NoSuchElementException if the requested group name is not defined
   */
def group(id: String): String = nameToIndex.get(id) match {
    case None => throw new NoSuchElementException("group name "+id+" not defined")
    case Some(index) => group(index)
  }

  /** The matched string; equivalent to `matched.toString` */
override def toString = matched

}