| by Arround The Web | No comments

How to Use Regex Whitespace in Java

Regex or Regular Expression is a set of special characters that combine to form a pattern to search characters in strings. In computer programming and software engineering, learning regex will be very helpful in finding information in any text. All kinds of text search, formatting, and text replacement operations can be carried out using regular expressions.

This tutorial will guide you about using the regex whitespace in Java.

What is Regex in Java?

A Regular Expression or Regex might be as simple as a single character or a complex pattern. It can be created with a string of text and symbols in a specific order. Most of the characters in a regex are letters and typographic symbols. Regex is case-sensitive, so keep that in mind while creating and using it.

How to Use Regex Whitespace in Java?

Although Java does not have any predefined Regular Expression class. However, we can use regular expressions by importing the “java.util.regex” library. It includes some classes such as “Pattern”, which is used for defining a regex pattern, and “Matcher” class which is used to search with the pattern.

There are two methods to use regex whitespace in Java as follows:

    • Using Pattern.matches() method (use predefined regex)
    • Using Pattern and Matcher class (create user-defined regex to match)

Let’s see how these methods will work with regex for whitespace in Java.

Method 1: Use Predefined Regex Whitespace with Pattern.matches() Method in Java

To find whitespaces in a string, there are three common regexes in Java:

    • \s: It represents a single white space.
    • \s+: It indicates multiple white spaces.
    • \u0020: It is the Unicode of the white space used as a regex to find whitespace in a text.

We can use these regexes in the static method “matches()” of the “Pattern” class. Pattern class belongs to the “java.util.regex” package. Below is the syntax of Pattern.matches() method is given:

Syntax

Pattern.matches("\s", " ");

 
The specified method takes two arguments: the regular expression and the string to match. The first argument “\s” is the regular expression or regex of the white space, and the second argument ” “ is the space in string. It returns either true or false as a boolean value.

Example 1: Use “\s” WhiteSpace Regex

Here, we will use the “\s” regex in the Pattern.matches() method. We will pass a string with no space in the method as a second argument. The method will check the regex and the string and then return a boolean value that will be stored in the “match” variable:

boolean match= Pattern.matches("\s", "");

 
Print the value of the match variable using the “System.out.println()” method:

System.out.println("Space exist:" + match);

 

The value returned by the “Pattern.matches()” method is “false” because the passed string has no space:


Now we will see some other examples to match whitespace with other regexes.

Example 2: Use “\s+” WhiteSpace Regex

In this example, we will pass the “\s+” regex in the “matches()” method to find multiple spaces:

boolean match= Pattern.matches("\s+", " ");

 
Print the value of the match variable that stores the returned result from the method:

System.out.println("Space exist:" + match);

 

As the second argument contains spaces, the resultant value is displayed as “true”:

Example 3: Use “\u0020” WhiteSpace Regex

Here, we will show you how Unicode is used as a regex in Java. For the specified purpose, we will use the “\u0020” regex as Unicode of the white space:

boolean match= Pattern.matches("\u0020", " ");

 
Print the returned value:

System.out.println("Space exist:" + match);

 

The Pattern.matches() method will print “true” as a passed string containing white spaces:


Let’s move to the other method to use regex in Java.

Method 2: Use User-defined Regex Whitespace With Pattern and Matcher class

The “Pattern” class is used to define or create a pattern, while the “Matcher” class is utilized to search according to the given pattern. The pattern for a regex can be created with the help of the “compile()” method of the Pattern class. It takes only one parameter, the pattern you want to compile for any purpose.

Syntax

Pattern.compile("\t\p{Zs}");

 
The Matcher class matches the pattern by using the “matcher()” method. It takes a “string” as the pattern.

Syntax

patternVariable.matcher(string);

 
There are some predefined regex for whitespaces that we have discussed above, the remaining are listed below:

    • \\t\\p{Zs}
    • \\p{Zs}

Now, let’s check out some examples.

Example 1: Use “\\t\\p{Zs}” WhiteSpace Regex

In this example, we will find out the number of whitespaces by counting them. First, we will create a String “s” and print it out on console:

String s = "WelcometoLinuxHint";
System.out.println(s);

 
Next, we will define a pattern “\\t\\p{Zs}” that acts as a whitespace regex in Java and is equal to “\s”.  After compiling the given pattern, variable “regexPattern” will contain resultant value:

Pattern regexPattern = Pattern.compile("\\t\\p{Zs}");

 
Call the “matcher()” method and pass “s” String:

Matcher stringSpaces = regexPattern.matcher(s);

 
Create an integer type variable “count” and initialize it with the value “0”:

int count = 0;

 
Count the number of whitespaces that exist in the string by using a “while” loop. The loop will traverse the String and increment the count variable value if it encounters any space:

while(stringSpaces.find()) {
count++;
}

 
Lastly, print the value of count to show how many spaces are found in a string:

System.out.println("String contains " +count+ " spaces");

 

Output


 
Example 2: Use “\p{Zs}” WhiteSpace Regex

Now, we will find the whitespaces in the string by using another pattern “\p{Zs}”. This pattern works similar to the “\s” and “\s+” regex:

Pattern regexPattern = Pattern.compile("\\p{Zs}");

 
Now, we call the “matcher()” method and pass “s” String as argument:

Matcher stringSpaces = regexPattern.matcher(s);

 
As in the above example, we also use a “while” loop to count the spaces in a string and print them:


 
The given output indicates that our String “Welcome to Linux Hint” contains three whitespaces:


 
We compile all the easiest methods that can help you to use regex whitespace in Java.

Conclusion

There are many regular expressions for whitespace such as “\s”, “\s+”, “\u0020”, “\\t\\p{Zs}”, and “\\p{Zs}”. These regexes are used in the matches() method of the Pattern class or by defining a pattern with the Pattern Class and matching it using the Matcher class. The most commonly used regex whitespace is \s and \s+. In this tutorial, we covered all the methods to use regex whitespace in Java.

Share Button

Source: linuxhint.com

Leave a Reply