scala 正则表达式_Scala正则表达式示例

本文介绍了Scala中的正则表达式,包括如何使用`Regex`类进行模式匹配,以及各种正则表达式运算符的用法,如`.`、`$`、`^`等,并提供了匹配和替换字符串的示例。
摘要由CSDN通过智能技术生成

scala 正则表达式

Regular expressions are pattern matching utilities found in most of the programming languages. They define a generic pattern to match a sequence of input characters. Regex are widely used in text parsing and search.

正则表达式是大多数编程语言中都可以找到的模式匹配实用程序。 它们定义了通用模式来匹配一系列输入字符。 正则表达式广泛用于文本解析和搜索。

The Regex class in scala is available in scala.util.matching package.

scala中的Regex类可在scala.util.matching包中找到。

Consider an example of how to find a word below.

考虑下面如何查找单词的示例。

import scala.util.matching.Regex

object findWord {
   def main(args: Array[String]) {
  	val p = "Functional".r
  	val st = "Scala is a Functional Programming Language"
 	 
  	println(p findFirstIn st)
   }
}

Below image shows the output produced when we execute this object main method.

下图显示了执行此对象main方法时产生的输出。

In the above example we are finding the word “functional” . We invoke the r() method which converts string to RichString and invokes the instance of Regex. The findFirstIn method finds the first occurrence of the pattern. To find all the occurrences use finadAllIn()</

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值