Scala FAQ: How can I use the startsWith
method on a Scala String
to match multiple possible patterns in a match
expression?
Solution
As shown in the following example, you can use the startsWith
method on a String
to match multiple possible patterns in a match
expression. startsWith
checks to see if a String
starts with the prefix (or substring) you specify, so although in these examples I use complete strings, you can also use regular expression patterns.
Example: startsWith + match expression
Here’s an example that shows how to use startsWith
in a Scala match
expression to match multiple possible patterns. I just used this code to generate the files on this website, and I can confirm that it works as desired:
def getSubtitleForUri(uri: String): String = uri match
case s if s.startsWith("/apple/") => "Apple tutorial"
case s if s.startsWith("/mac/") => "Mac tutorial"
case s if s.startsWith("/macos/") => "Mac tutorial"
case s if s.startsWith("/mac-os-x/") => "Mac tutorial"
case s if s.startsWith("/java/") => "Java tutorial"
case s if s.startsWith("/perl/") => "Perl tutorial"
case s if s.startsWith("/ruby/") => "Ruby tutorial"
case s if s.startsWith("/scala/") => "Scala tutorial"
case s if s.startsWith("/unix/") => "Unix tutorial"
case s if s.startsWith("/linux-unix/") => "Linux/Unix tutorial"
case _ => "Alvin Alexander tutorials"
As you might guess from the names, that match
expression checks the URI that it’s given as a String
input parameter, and tests to see what the URI starts with. It then returns a suitable “sub-title” string for each of the URIs shown. In the last case
statement, if there is no match with those startsWith
tests, a default sub-title is shown.
In summary, if you wanted to see how to perform startsWith
cases with a Scala match
expression, I hope this example is helpful.
Matching multiple patterns on one line
When you look at that example you’ll see that I have several string patterns that begin with "/mac"
. Therefore, if you want to use just one line to match all those patterns, that’s just fine. In that case you just need this one pattern match:
case s if s.startsWith("/mac") => "Mac tutorial"
I demonstrate that all of my examples work in this Scala REPL example:
scala> "/macos".startsWith("/mac") val res0: Boolean = true scala> "/mac-os-x".startsWith("/mac") val res1: Boolean = true scala> "/mac".startsWith("/mac") val res2: Boolean = true
Note that this isn’t a regular expression, but it starts to give you an idea of what you can do with regular expressions, startsWith
, and Scala match
expressions.
Using a regular expression in a match expression case statement
Note that in situations where you really need to use a regular expression you can use the matches
method of the String
class, like this:
scala> "/mac-os-x".matches("/mac.*") val res3: Boolean = true scala> "/macos".matches("/mac.*") val res4: Boolean = true scala> "/mac/".matches("/mac.*") val res5: Boolean = true
However, for my particular situation I don’t need a regular expression, so I think my earlier examples are a better solution in this case.