user2628641 user2628641 - 1 month ago 7
Scala Question

Scala, regex matching ignore unnecessary words

My program is:

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

val outputFieldMod = "TRASHprefix_target_TRASH"

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

println(tar)


Basically, I try to get the "target" and ignore "TRASH" (I used *). But it has some error and I am not sure why..

Answer

Simple and straight forward standard library function (unanchored)

Use Unanchored


Solution one

Use unanchored on the pattern to match inside the string ignoring the trash

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

unanchored will only match the pattern ignoring all the trash (all the other words)

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

Example

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

Solution two

Pad your pattern from both sides with .*. .* matches any char other than a linebreak character.

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

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

Example

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
Comments