JAVA DEVELOPER SITE
 

Scala - Regular Expressions


In Scala - Regular Expressions chapter Scala supports regular expressions through Regex class available in the scala.util.matching package.


import scala.util.matching.Regex

object Demo {
   def main(args: Array[String]) {
      val pattern = "Scala".r
      val str = "Scala is Scalable and good language "
      
      println(pattern findFirstIn str)
   }
}
Output
Scala
we can use the findAllIn( ) method and in case there are multiple Scala words available in the target string, this will return a collection of all matching words.

Example

import scala.util.matching.Regex

object Demo {
   def main(args: Array[String]) {
      val pattern = new Regex("(S|s)cala")
      val str = "Scala is scalable and good language "
      
      println((pattern findAllIn str).mkString(","))
   }
}

Output

Scala,scala
If you would like to replace matching text, we can use replaceFirstIn( ) to replace the first match or replaceAllIn( ) to replace all occurrences.
object Demo {
   def main(args: Array[String]) {
      val pattern = "(S|s)cala".r
      val str = "Scala is scalable and cool"
      
      println(pattern replaceFirstIn(str, "Java"))
   }
}

Output

Java is scalable and  good language

Forming Regular Expressions

Subexpression Matches
^ Matches beginning of line.
$ Matches end of line.
. Matches any single character except newline. Using m option allows it to match newline as well.
[...] Matches any single character in brackets.
[^...] Matches any single character not in brackets
\\A Beginning of entire string
\\z End of entire string
\\Z End of entire string except allowable final line terminator.
re* Matches 0 or more occurrences of preceding expression.
re+ Matches 1 or more of the previous thing
re? Matches 0 or 1 occurrence of preceding expression.
re{ n} Matches exactly n number of occurrences of preceding expression.
re{ n,} Matches n or more occurrences of preceding expression.
re{ n, m} Matches at least n and at most m occurrences of preceding expression.
a|b Matches either a or b.
(re) Groups regular expressions and remembers matched text.
(?: re) Groups regular expressions without remembering matched text.
(?> re) Matches independent pattern without backtracking.
\\w Matches word characters.
\\W Matches nonword characters.
\\s Matches whitespace. Equivalent to [\t\n\r\f].
\\S Matches nonwhitespace.
\\d Matches digits. Equivalent to [0-9].
\\D Matches nondigits.
\\A Matches beginning of string.
\\Z Matches end of string. If a newline exists, it matches just before newline.
\\z Matches end of string.
\\G Matches point where last match finished.
\\n Back-reference to capture group number "n"
\\b Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
\\B Matches nonword boundaries.
\\n, \\t, etc. Matches newlines, carriage returns, tabs, etc.
\\Q Escape (quote) all characters up to \\E
\\E Ends quoting begun with \\Q