Scala

Scala Regex

Regex in the Scala programming language stands for Regular Expression. Such an expression is extensively used for pattern matching, i.e. a sequence of characters is defined as a pattern, and this pattern is searched across any given input text. This guide will learn to use Scala Regex in Ubuntu 20.04.

What is Regex in the Scala Programming Language?

Regex in Scala serves the same purpose as Regular Expression in any other programming language. This function is implemented within the scala.util.matching.Regex library can be used very conveniently to find any pattern occurrences within the given text. This function is used in conjunction with the “find” or “replace” functions of Scala for finding and replacing the matching pattern, respectively. Moreover, you are free to use this pattern matching for only the first occurrence of the pattern within the given text or all of its occurrences.

How to use Scala Regex?

For using Scala Regex for pattern matching, you can see the following examples that we have furnished for you:

Example # 1: Using Scala Regex for Finding the First Occurrence of the Desired Pattern:

At times, you only want to find out the first occurrence of a pattern from any given string. To do so, you can take help from the Scala script shown in the image below:

We have the main class named “Regex” inside which we have defined our “main()” function. Then, we have defined a random pattern whose occurrence we want to look for within an input string. While defining this pattern, we have called the “r” function with it that will perform the actual pattern matching. Then, we have declared a value named “input” and assigned a string in which we want to find the matching pattern. Finally, we have used the “findFirstIn” function within our “println” statement to print the first occurrence of the matching pattern on the terminal.

The command shown below was used for compiling this Scala script:

$ scalac Regex.Scala

Then, we have used the following execution command with the class name for executing this Scala script:

$ scala Regex

You can see the first occurrence of the specified pattern in the image shown below. The statement “Some(Scala)” represents the first occurrence of the pattern specified in our Scala script.

Example # 2: Using Scala Regex for Finding all the Occurrences of the Desired Pattern:

At times, you want to find out all the occurrences of the matching pattern from a given string. For that, you can take help from the following Scala code:

In this example, we have used the same pattern for matching that we used in our first example. However, we have slightly changed the input string in a way that the given pattern occurs twice in our input string. Now, to print all of these occurrences of the matching pattern on the terminal, we have used the “findAllIn” method instead of the “findFirstIn” method in the “println” statement. Moreover, we have also used the “mkString” method for separating out the multiple occurrences of the matching pattern with commas while displaying them on the terminal. You can use any other character instead of a comma to separate out the words in your output.

After compiling and executing this Scala script, all the occurrences of the matching pattern appeared on the terminal, as shown in the image below:

Example # 3: Using Scala Regex for Pattern Matching Using Both Upper and Lower Cases:

Sometimes, instead of just looking for a pattern beginning with a single case, we want to look for it for both the upper and lower cases. For that, you need to take a look at the following image:

In this example, our pattern is the same as we have used in the examples above; however, for finding the matching pattern beginning with both cases, we have separated “S” and “s” from each other with the help of a pipe while defining this pattern. Also, we have slightly modified our input string in a way that the given pattern occurs both in upper and lower cases within our input string. Then, we have used the “findAllIn” method for printing out all the occurrences of the matching pattern on the terminal.

You can witness from the image shown below that the above-mentioned Scala script managed to display all the occurrences of the matching pattern on the terminal regardless of their case.

Example # 4: Using Scala Regex for Finding and Replacing the Given Pattern:

Finally, you might not just want to find a matching pattern; rather, you might also want to replace all of its occurrences with some other word or phrase. The following Scala script will help you out in achieving this sort of functionality:

In this example, we have the same pattern that we used in our third example and the same input string. However, while calling the “println” command, instead of using the “findAllIn” method, we have used the “replaceAllIn” method and passed it the given input string and also the string with which we want to replace all the occurrences of the matching pattern. If you just want to replace the first occurrence of the matching pattern, then instead of using the “replaceAllIn” method, you should use the “replaceFirstIn” method.

You can see from the image shown below that our Scala script has successfully managed to find and replace the matching pattern with a new string.

Conclusion:

This article was all about using the Regex method of this Scala programming language. This function is used extensively for pattern matching from a given input string. To elaborate its usage in the Scala programming language, we explained four different examples for you that presented multiple variations of using this function in Scala. By going through these variations, you can easily understand the Regex function of the Scala programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.