Scala,正则表达式匹配忽略不必要的单词

时间:2016-10-29 16:33:40

标签: regex scala

我的节目是:

val pattern = "[*]prefix_([a-zA-Z]*)_[*]".r

val outputFieldMod = "TRASHprefix_target_TRASH"

var tar =
  outputFieldMod match {
    case pattern(target) => target
  }

println(tar)

基本上,我试图获得"目标"并忽略" TRASH" (我用了 *)。但它有一些错误,我不知道为什么..

2 个答案:

答案 0 :(得分:2)

简单直接的标准库函数(未锚定)

使用 Unanchored

解决方案一

在模式上使用unanchored以匹配字符串内部而忽略垃圾

val pattern = "prefix_([a-zA-Z]*)_".r.unanchored

unanchored只会匹配忽略所有垃圾的模式(所有其他单词)

val result = str match {
 case pattern(value) => value
 case _ => ""
}

示例

Scala REPL

scala> val pattern = """foo\((.*)\)""".r.unanchored
pattern: scala.util.matching.UnanchoredRegex = foo\((.*)\)

scala> val str = "blahblahfoo(bar)blahblah"
str: String = blahblahfoo(bar)blahblah

scala> str match { case pattern(value) => value ; case _ => "no match" }
res3: String = bar

解决方案二

使用.*从两侧填充您的图案。 .*匹配除换行符之外的任何字符。

val pattern = ".*prefix_([a-zA-Z]*)_.*".r

val result = str match {
   case pattern(value) => value
   case _ => ""
}

示例

Scala REPL

scala> val pattern = """.*foo\((.*)\).*""".r
pattern: scala.util.matching.Regex = .*foo\((.*)\).*

scala> val str = "blahblahfoo(bar)blahblah"
str: String = blahblahfoo(bar)blahblah

scala> str match { case pattern(value) => value ; case _ => "no match" }
res4: String = bar

答案 1 :(得分:1)

这将有效,val pattern = ".*prefix_([a-z]+).*".r,但它通过大写/小写字母区分目标和垃圾。无论从垃圾数据中确定真实的目标数据是什么,都将决定真正的正则表达式模式。