The Wayback Machine - https://web.archive.org/web/20221105190442/https://www.geeksforgeeks.org/scala-string-matches-method-with-example/
Skip to content
Related Articles

Related Articles

Scala String matches() method with example

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Oct, 2019
View Discussion
Improve Article
Save Article

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 program of int matches()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying matches method
        val result = "Preeti".matches(".*i")
          
        // Displays output
        println(result)
          
    }
} 

Output:

true

So, here the regular expression matches the string stated. So, it returns true.
Example #2:




// Scala program of int matches()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying matches method
        val result = "Preeti".matches(".i.*")
          
        // Displays output
        println(result)
      
    }
} 

Output:

false

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!