What are Regular Expressions?

Regular expressions are patterns that permit you to “match” various string values in a variety of ways. Scala uses import scala.util.matching.Regex to implement regular expression concept.

We have the perfect professional Scala and Apache Spark Training Course for you!

A pattern is simply one or more characters that represent a set of possible match characters. In regular expression matching, you use a character or set of characters to represent the strings you want to match in the text. A regular expression is a way of describing a set of strings using common properties, for example, strings that start with an “A” and end with an exclamation mark.

Want to get certified in Scala! Learn Scala from top Scala experts and excel in your career with Intellipaat’s Scala certification!

Watch this Apache-Spark-Scala video

Scala Regular Expressions What are Regular Expressions? Regular expressions are patterns that permit you to “match” various string values in a variety of ways. Scala uses import scala.util.matching.Regex to implement regular expression concept. We have the perfect professional Scala and Apache Spark Training Course for you! A pattern is simply one or more characters

Procedure

1. Change the string representation of a regular expression into a Pattern object;

2. Create a Matcher object from the object which is created in the first step that applies to a particular string;

Still, have queries? Come to Intellipaat’s Big Data Community, clarify all your doubts, and excel in your career!

3. Apply the various methods of the Matcher object to the particular string.

These steps can be expressed in Scala as follows:

import java.util.regex.{Pattern,Matcher}
. . . . . . . . . . . . . . . . . . . .
val i = Pattern.compile("regularExpression")
val j = i.matcher(String)
var foundMatch = j.find()

Looking for a job change in Scala? Check out this informative blog on Jobs in Scala and excel in your career!

e.g.

import java.util.regex._
object HelloWorld {
def main(args: Array[String])  {
val p = Pattern.compile("i")
val m = p.matcher("intellipaat")
var found = false
while (m.find()) {
print("I found the text \""+ m.group())
print("\" starting at index " + m.start()+"\n")
found = true
}
if (!found)
println("No match found.")
}
}

Output

I found the text “i” starting at index 0

I found the text “i” starting at index 6

Interested in learning Scala? Check out the Scala Training in Sydney!

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *