...

Cloud Computing - SCALA

Back to Course

Lesson Description


Lession - #695 SCALA REGULAR EXPRESSION


SCALA Regular Expression

This part makes sense of how Scala support regular expression through Regex class accessible in the scala.util.matching package.

try the following model program where we will try to figure out word Scala from an statement.

Model
import scala.util.matching.Regex

object Demo {
   def main(args: Array[String]>
{ val pattern = "Scala".r val str = "Scala is Scalable and cool" println(pattern findFirstIn str>
} }


Save the above program in Demo.scala. The following commands are utilized to compile and execute this program.
command

\>scalac Demo.scala
\>scala Demo

Yield

Some(Scala>



We make a String and call the r( >
method on it. Scala verifiably switches the String over completely to a RichString and invokes that method to catch an instance of Regex. To track down a first match of the standard expression, basically call the findFirstIn(>
method. If rather than observing just the main event we might want to observe all events of the matching word, we can utilize the findAllIn( >
method and on the off chance that there are various Scala words accessible in the objective string, this will return an collection of every matching word.

You can utilize the mkString( >
method to concatinate the resulting list and you can utilize a line (|>
to look through little and capital instance of Scala and you can utilize Regex constructor instead or r(>
method to make an example.

try the following model program.

Model
import scala.util.matching.Regex

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



Save the above program in Demo.scala. The following commands are utilized to compile and execute this program.
Command

\>scalac Demo.scala
\>scala Demo


Yield

Scala,scala


Assuming you might want to replace matching text, we can utilize replaceFirstIn( >
to replace the first match or replaceAllIn( >
to replace all events.

Model
object Demo {
   def main(args: Array[String]>
{ val pattern = "(S|s>
cala".r val str = "Scala is adaptable and cool" println(pattern replaceFirstIn(str, "Java">
>
} }



Save the above program in Demo.scala. The following commands are utilized to compile and execute this program.


Command

\>scalac Demo.scala
\>scala Demo

Yield


Java is adaptable and cool