Using Extractors with Pattern Matching In Scala Last Updated : 02 Aug, 2019 Comments Improve Suggest changes Like Article Like Report Scala Extractor is defined as an object which has a method named unapply as one of its part. Extractors can be utilized in Pattern Matching. The unapply method will be executed spontaneously, While comparing the Object of an Extractor in Pattern Matching. Below is the example of Extractor with pattern matching. Example #1: Scala // Scala program of extractors // with pattern matching // Creating object object GfG { // Main method def main(args: Array[String]) { // Assigning value to the // object val x = GfG(25) // Displays output of the // Apply method println(x) // Applying pattern matching x match { // unapply method is called case GfG(y) => println("The value is: "+y) case _ => println("Can't be evaluated") } } // Defining apply method def apply(x: Double) = x / 5 // Defining unapply method def unapply(z: Double): Option[Double] = if (z % 5 == 0) { Some(z/5) } else None } Output: 5.0 The value is: 1.0 In above example, object name is GFG also we are using unapply method and applying case class with match expression. Example #2: Scala // Scala program of extractors // with pattern matching // Creating object object GFG { // Main method def main(args: Array[String]) { val x = GFG(15) println(x) x match { case GFG(num) => println(x + " is bigger two times than " + num) // Unapply is invoked case _ => println("not calculated") } } def apply(x: Int) = x * 2 def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z/2) else None } Output: 30 30 is bigger two times than 15 When comparing an extractor object using the match statement the unapply method will be automatically executed. Note: A Case class already has an Extractor in it so, it can be utilized spontaneously with Pattern Matching. Comment More infoAdvertise with us Next Article Using Extractors with Pattern Matching In Scala D DivyaPareek Follow Improve Article Tags : Scala Similar Reads Scala String regionMatches() method with example The regionMatches() method is used to check if two string regions are equal or not. However, if ignoreCase is stated true in the argument list then the case difference is ignored. Method Definition: Boolean regionMatches(boolean ignoreCase, int toffset, String other, int offset, int len) Return Type 1 min read Scala | Pattern Matching Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C. Here, "match" keyword is used inste 3 min read Scala String matches() method with example The matches() method is used to check if the string stated matches the specified regular expression in the argument or not. Method Definition: Boolean matches(String regex) Return Type: It returns true if the string matches the regular expression else it returns false. Example #1: Scala // Scala pro 1 min read Scala String replaceFirst() method with example The replaceFirst() method is same as replaceAll but here only the first appearance of the stated sub-string will be replaced. Method Definition: String replaceFirst(String regex, String replacement) Return Type: It returns the stated string after replacing the first appearance of stated regular expr 1 min read Scala String replaceAll() method with example The replaceAll() method is used to replace each of the stated sub-string of the string which matches the regular expression with the string we supply in the argument list. Method Definition: String replaceAll(String regex, String replacement) Return Type: It returns the stated string after replacing 1 min read Like