user2628641
user2628641

Reputation: 2154

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..

Upvotes: 2

Views: 831

Answers (2)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

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

Upvotes: 3

jwvh
jwvh

Reputation: 51271

This will work, val pattern = ".*prefix_([a-z]+).*".r, but it distinguishes between target and trash via lower/upper-case letters. Whatever determines real target data from trash data will determine the real regex pattern.

Upvotes: 1

Related Questions