如何确定字符串是否在Scala中包含正则表达式?

In Scala, data validation and adding constraints to its usage is important for data processing. And checking if a regular expression is present in a string.

在Scala中,数据验证和对其使用增加约束对数据处理很重要。 并检查字符串中是否存在正则表达式。

To check whether the strings contain a regular expression or regex we will use the findFirstIn() method.

为了检查字符串是否包含正则表达式或正则表达式,我们将使用findFirstIn()方法。

Syntax:

句法:

    regex.findfirstIn(string)

The method accepts the string in which regex is searched and returns an Option[String] value.

该方法接受在其中搜索正则表达式的字符串 ,并返回Option [String]值。

程序检查字符串是否包含正则表达式 (Program to check if a string contains a regular expression)

object myObject {
    
    def main(args: Array[String]) {
        val bike = "harley davidson Iron 883"    
        val numRegex = "[0-9]+".r

        println("String: "+bike)

        val matchPattern = numRegex.findFirstIn(bike)

        print("Pattern found "+matchPattern)
    }
}

Output

输出量

String: harley davidson Iron 883
Pattern found Some(883)

Explanation:

说明:

In this program, we have a string with is to be checked for regular expression which is created using the .r method. The method findFirstIn() is used to check for the presence of the pattern in the string which returns a none if no match is found and the matched string if a match is found.

在此程序中,我们将使用.r方法创建一个字符串,用于检查正则表达式。 方法findFirstIn()用于检查字符串中是否存在模式,如果未找到匹配项,则返回none;如果找到匹配项,则返回匹配的字符串。

翻译自: https://www.includehelp.com/scala/how-to-determine-if-a-string-contains-a-regular-expression-in-scala.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值