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]可以有的操作函数:

trait MatchData {

  /** 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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值