regex-expressions-in-java,-s-vs.-s+

A Regex (Regular Expression) serves to specify a search pattern for text. It is frequently utilized for pattern identification, validation, searching and substitution, text interpretation, string division, and more. Java includes a robust regex (regular expression) API via the java.util.regex package. Among the various regex patterns, s and s+ are the most widely employed to manage whitespaces.

In this article, we shall explore regex expressions (s and s+) in greater depth.

Table of Contents:

What does s mean in Java Regex?

In Java, s is a built-in character class that corresponds to a single whitespace character. It is utilized to recognize and manipulate whitespace within strings, for instance, separating text, trimming unnecessary spaces, or validating input formats.

Illustration of s in Java

Code:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String text = "Hello Intellipaat";
        String regex = "HellosIntellipaat";
        boolean matches = Pattern.matches(regex, text);
        if(matches)
        System.out.println("Strings are equivalent"); 
        else
          System.out.println("Strings are not equivalent");
    }
}

Result:

Strings are equivalent

Explanation: In this case, the text string has whitespace, while the regex string does not actually include a whitespace but instead represents one whitespace character in a regular expression through s. Following this, we verify if the text aligns with the regex pattern using a regex matching function, and indeed, it confirms that both strings are precisely equivalent to what was anticipated.

What does s+ signify in Java Regex?

In contrast to s, which identifies solely one whitespace character, s+ corresponds to one or more contiguous whitespace characters. The + quantifier indicates “one or more instances” of the preceding character class.

Illustration of s+ in Java Regex

Code:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String text = "Hello         Intellipaat";
        String regex = "Hellos+Intellipaat";
        boolean matches = Pattern.matches(regex, text);
        if(matches)
        System.out.println("Strings are equivalent"); 
        else
          System.out.println("Strings are not equivalent");
    }
}

Result:

Strings are equivalent

Explanation: The s+ in “Hellos+Intellipaat” permits one or more whitespace characters, allowing the strings to match and thus become equivalent.

Primary Differences Between s and s+

Below are the distinctions between s and s+:

Regex Definition
s Matches only a single whitespace character.
s+ Matches multiple whitespace characters.

Practical Examples of s versus s+ in Java

Here are some real-world applications of s and s+ in Java:

Example 1: Dividing Strings by Whitespace using s

If you wish to separate a string into words using single whitespace characters, consider the following example:

Code:

import java.util.regex.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "Intellipaat is the best organization";
        String[] words = str.split("s");
        System.out.println(Arrays.toString(words));
    }
}

Result:

[Intellipaat, is, the, best, organization]

Example 2: Substituting Multiple Spaces(s+) with a Solo Space

Code:

import java.util.regex.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "Intellipaat   is    the best   organization";
        String result = str.replaceAll("s+", " ");
        System.out.println(result);
    }
}

Result:

Intellipaat is the best organization

Wrap-Up

In this article, we have examined regex expressions (s and s+) in Java. Use s when working with a single whitespace, while s+ is necessary when you require one or more whitespaces. If you aspire to advance your career in Java, please check out our advanced Java course.

FAQs

The post Regex expressions in Java, s vs. s+ was first published on Intellipaat Blog.


Leave a Reply

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

Share This