2

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

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

A dot in Scala regex will match any char other than a linebreak character. Not just alphanumeric or spaces.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.