scala之正则表达式(二)内部匹配函数
2016-08-02 20:30
495 查看
1、scala解析正则表达式步骤
以下面表达为例:
图1:提取匹配信息
在上面的基础上我们在来做一些事情,如下:
2、findAllIn函数
功能:把全部的匹配值找出
3、findFirstIn函数
功能:把第一个的匹配值找出
4、findAllMatchIn函数
5、findPrefixOf函数
功能:找出前缀为正则表达式
6、findPrefixMatchOf函数
比较
1、findAllIn和findAllMatchIn的区别
也就是返回类型Option[Match]和Option[String]的区别
(1)类型上的区别
可以说findAllMatchIn比findAllIn更加抽象,功能更加多样,findAllMatchIn可以在返回类型Match中可以增加一些操作
比如从那个位置开始,那个位置结束的。并可以提取他们的值。
以下是 返回Option[Match]可以有的操作函数:
以下面表达为例:
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 }
相关文章推荐
- 使用正则表达式匹配JS函数代码
- 【shell脚本】匹配函数的正则表达式
- PHP 正则表达式匹配函数 preg_match 与 preg_match_all
- 正则表达式的匹配函数
- scala 正则表达式匹配子串
- 给xpath添加正则表达式匹配函数
- Scala中正则表达式以及与模式匹配结合
- scala基础31-模式匹配之正则表达式
- Scala中的正则表达式、与模式匹配结合的的Reg代码实战
- scala 中文匹配正则表达式
- C#中几个正则表达式匹配输入字符的函数
- [导入]匹配正则表达式的函数BOOL型(修正一下)
- oracle正则表达式函数 匹配
- scala之正则表达式(一)基础匹配
- Scala - 正则表达式匹配例子
- 探讨PHP ereg()函数与正则表达式的匹配
- Vim自定义函数之正则表达式匹配
- Scala深入浅出实战经典:19,Scala中的正则表达式、与模式匹配结合的的Reg代码实战
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- 正则表达式:.Net Framework平衡组/递归匹配搜索源码中的函数/方法({}匹配)