Wednesday, September 16, 2009

Matching Regular expressions

This topic is derived from the blog post: Using pattern matching with regular expressions in Scala

The Regex class in Scala provides a very handy feature that allows you to match against regular expressions. This makes dealing with certain types of regular expression very clean and easy to follow.

What needs to be done is to create a Regex class and assign it to a val. It is recommended that the val starts with an Uppercase letter, see the topic of matching about the assumptions matching makes based on the first letter of the Match case clause.

There is nothing like examples to help explain an idea:
  1. // I normally use raw strings (""") for regular expressions so that I don't have to escape all \ characters
  2. // There are two ways to create Regex objects.
  3. // 1. Use the RichString's r method
  4. // 2. Create it using the normal Regex constructor
  5. scala> val Name = """(\w+)\s+(\w+)""".r
  6. Name: scala.util.matching.Regex = (\w+)\s+(\w+)
  7. scala> import scala.util.matching._
  8. import scala.util.matching._
  9. // Notice the val name starts with an upper case letter
  10. scala> val Name = new Regex("""(\w+)\s+(\w+)""")
  11. Name: scala.util.matching.Regex = (\w+)\s+(\w+)
  12. scala> "Jesse Eichar"match {
  13.      | case Name(first,last) => println("found: ", first, last)
  14.      | case _ => println("oh no!")
  15.      | }
  16. (found: ,Jesse,Eichar)
  17. scala> val FullName = """(\w+)\s+(\w+)\s+(\w+)""".r
  18. FullName: scala.util.matching.Regex = (\w+)\s+(\w+)\s+(\w+)
  19. // If you KNOW that the match will work you can assign it to a variable
  20. // Only do this if you are sure the match will work otherwise you will get a MatchError
  21. scala> val FullName(first, middle, last) = "Jesse Dale Eichar"
  22. first: String = Jesse
  23. middle: String = Dale
  24. last: String = Eichar

3 comments:

  1. Why the val name starts with an upper case letter ?

    ReplyDelete
  2. Thanks for making me review this. In scala 2.7 if you wanted to use an object for matching, the val name had to start with an uppercase letter. so

    val fullName(first, middle, last) = "Jesse Dale Eichar"

    would not compile. In scala 2.8 that seems to have changed now it does compile and works as expected.

    Although I still recommend to uppercase the letter to indicate that it is going to be used for matching.

    ReplyDelete
  3. thank you Jesse! you are great!

    ReplyDelete