Table of Contents
Regex is a common name for coders, from beginners to professionals. This combination of characters is often used to offer users a search pattern to look for specific ones in a string.
This tutorial will focus on discussing the best ways to use regex to find whitespaces in Java.
What Is Regex?
Regex is also known as RegExp \s Java metacharacter, which is an API to define string patterns. You can use it to search, manipulate, and edit a Java string. Email passwords and validation are just a few strings that Regex proves its utility.
This expression is often provided under the java.util.regex
package with one interface and three classes. They are:
- Pattern class: this one is used to define string patterns.
- Matcher class: it performs match text operation with patterns.
- PatternSyntaxException Class: it indicates a regular expression’s syntax error.
- MatchResult Interface: it represents a match operation’s result.
Each class includes a wide range of syntaxes to serve different programming and processing purposes.
To use the RegExp constructor, you need to type \\
. This indicates a single backslash.
On the other hand, a whitespace in a regular expression can be formfeed (\f
), vertical tab (\v
), return (\r
), newline (\n
), non-breaking space (\xA0
), and space.
How To Use Regex To Find WhiteSpaces In Java
Among the wide range of syntaxes in Regex, you need to use the class Pattern’s matches()
method to find whitespaces. This method shows two different arguments, including the matching string and the regular expression.
Plus, the most common character to use in this case is \s+
and \s
. The main difference between them is the number of whitespace they represent. While the former shows various whitespaces in a Java string, the latter one indicates only one character.
Our example employs the regex \ s+
to test a string with three whitespaces. If the whitespaceMatcher1 output is True, then the pattern can match and find the whitespaces.
With whitespaceMatcher2, the \s
regex identifies a single one and returns the string “ ”. Remember that the \s
and \S
is not the same in a case-sensitive regular expression.
The [\\t\\p{Zs}
] regex works the same way as \s
and will return true for a whitespace. If the string containing these characters is passed, the Unicode character represents space and the output will be true.
The last \p{Zs}
regex is another whitespace separator.
Code:
import java.util.regex.Pattern;
public class RegWhiteSpace {
public static void main(String[] args) {
boolean whitespaceMatcher1 = Pattern.matches("\\s+", " ");
boolean whitespaceMatcher2 = Pattern.matches("\\s", " ");
boolean whitespaceMatcher3 = Pattern.matches("[\\t\\p{Zs}]", " ");
boolean whitespaceMatcher4 = Pattern.matches("\\u0020", " ");
boolean whitespaceMatcher5 = Pattern.matches("\\p{Zs}", " ");
System.out.println("\\s+ ----------> " + whitespaceMatcher1);
System.out.println("\\s -----------> " + whitespaceMatcher2);
System.out.println("[\\t\\p{Zs}] --> " + whitespaceMatcher3);
System.out.println("\\u0020 ------->" + whitespaceMatcher4);
System.out.println("\\p{Zs} ------->" + whitespaceMatcher5);
}
}
Output:
\s+ ----------> true
\s -----------> true
[\t\p{Zs}] --> true
\u0020 ------->true
\p{Zs} ------->true
Conclusion
In Java, there are various methods to find whitespaces with a regular expression. A whitespace can be anything, including a tab character, new line, space, and vertical tab.
So check our article to learn to use regex to find whitespaces in Java.
Leave a comment