coder25
coder25

Reputation: 2393

How to match a string against a list of regex expression

List3 contains the list of element that are regular expression pattern fetched from DB. I want to print Success if any of the regex matches else Failure

val list3 = List(Test("A..C.."), Test("DF...."))
val testExpr ="DF1C13"
val s3 = list3
    .filter(x => x.s.r.pattern.matcher(testExpr).matches)
    .map(_ => "Success")

Actual Output

 List(Success)

Expected Simple String

"Success" //Successful match
"Failure" //If match fails

Upvotes: 2

Views: 437

Answers (3)

The fourth bird
The fourth bird

Reputation: 163207

You could map and test the outcome of the expression and omit the filter part, or else you will remove them from the collection.

val list3 = List(Test("A..C.."), Test("DF...."))
val testExpr ="DF1C13"
val s3 = list3
    .map(x => if(x.s.r.pattern.matcher(testExpr).matches) "Success" else  "Failure")

Output

s3: List[String] = List(Failure, Success)

You could also keep the filter as matches returns a boolean, and omit the map. Then check if the s3 collection is not empty and return either Success or Failure as a string.

val list3 = List(Test("A..C.."), Test("DF...."))
val testExpr ="DF1C13"
val s3 = list3
.filter(x => x.s.r.pattern.matcher(testExpr).matches)

if (s3.nonEmpty) "Success" else "Failure"

Output

res0: String = Success

Upvotes: 0

Alfilercio
Alfilercio

Reputation: 1118

if you only need to know if it satifies one of the regex, you can go for exists, instead of filter and map, it will return a true if any of the regex is true, and false if none of them are true:

val resulBool: Boolean = list3
    .exists(x => x.s.r.pattern.matcher(testExpr).matches)
val s3 = if (resulBool) "Success" else "Failure"

But if you want that the expression satisfies all the regex, you can go for forAll, that will only return true if all the regex are true

val resulBool: Boolean = list3
    .forall(x => x.s.r.pattern.matcher(testExpr).matches)
val s3 = if (resulBool) "Success" else "Failure"

An example of exists and forall here

Upvotes: 2

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

You can use .forall to run predicate against all values of a collection

if (list3.forall(x => x.s.r.pattern.matcher(testExpr).matches)) "Success" else "Failure"
// "Failure"

or if inside a .map if you want to have a result of every match as suggested by @The fourth bird:

list3.map(x => if (x.s.r.pattern.matcher(testExpr).matches) "Success" else "Failure")
// List("Failure", "Success")

or .exists if you want to check if at least one matcher works as suggested by @Alfilercio

if (list3.exists(x => x.s.r.pattern.matcher(testExpr).matches)) "Success" else "Failure"
// "Success"

BTW, if you plan to reuse these regexp after fetching them from DB, I would suggest to not recompute them every time as this is expensive.

val patterns = list3.map(x => x.s.r.pattern)

if (patterns.forall(x => x.matcher(testExpr).matches)) "Success" else "Failure"

Upvotes: 1

Related Questions