Salim Fadhley Salim Fadhley - 1 year ago 87
Scala Question

Does Scala provide an idiomatic way to partially match strings other than regular expressions?

This bit of code implements a mini string matching language - which understands two symbols (?=match any char, *=match one or more char). This code seems to work just fine, but it seems a little bit inelegant.

object Re {
def check(search: String, input: String):Boolean = {
search match {
case `input` => true
case x if x.startsWith("?") =>
check(x.stripPrefix("?"), input.tail)
case x if x.startsWith("*") => input match {
case "" => false
case i => check(x.stripPrefix("*"), i.tail) | check(x, i.tail)
case _ => false

Specifically, I dislike the cases where I say x if x.startswith(something) and then have to strip that something.

Does scala have a more idiomatic way to do this? Something along the lines of how the Seq matcher works so that I don't need startsWith or stripPrefix.

Answer Source

No to my best knowledge. So i would rather stick to sequences pattern matching:

  def check(search: String, input: String): Boolean = check(search.toList, input.toList)

  def check(search: List[Char], input: List[Char]): Boolean = {
    search match {
      case _ if search == `input` => true
      case '?' :: tail => check(tail, input.tail)
      case '*' :: tail => if (input.isEmpty) false else check(tail, input.tail) | check(search, input.tail)
      case _ => false

It might be not so elegant solution as in @0__ answer but in case this function is going to change in the future IMO it would be better to have capabilities of sequence pattern matching.

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download