Javatpoint Logo
Javatpoint Logo

Quantifiers in Java

In Java, regular expressions are frequently employed to define search patterns using sequences of characters. Quantifiers, which determine the number of occurrences of a character or group of characters, are integral to specifying the extent of the search. These expressions help define the rules for pattern matching within the given text or data.

  1. X* (Zero or More Occurrences):
    • The quantifier specifies that the element X may appear zero or more times in the given string.
    • Example: If X is the character 'a', the pattern "a*" would match an empty string, "a", "aa", "aaa", and so forth.
  2. X? (Zero or One Occurrence):
    • The quantifier indicates that the element X may appear zero or one time in the given string.
    • Example: If X is the character 'b', the pattern "b?" would match an empty string or "b".
  3. X+ (One or More Occurrences):
    • The quantifier specifies that the element X must appear at least once in the given string, but it can also appear more times.
    • Example: If X is the character 'c', the pattern "c+" would match "c", "cc", "ccc", and so on.
  4. X{n} (Exactly n Occurrences):
    • The quantifier mandates that the element X appear exactly n times in the given string.
    • Example: If X is the character 'd', the pattern "d{3}" would match "ddd".
  5. X{n,} (At Least n Occurrences):
    • The quantifier specifies that the element X must appear at least n times in the given string, but it can also appear more times.
    • Example: If X is the character 'e', the pattern "e{2,}" would match "ee", "eee", and so forth.
  6. X{n, m} (Occurrences Between n and m):
    • The quantifier defines a range, stating that the element X must appear between n and m times in the given string.
    • Example: If X is the character 'f', the pattern "f{2,4}" would match "ff", "fff", or "ffff", but not "f" or "fffff".

Greedy Quantifier (Default)

A greedy quantifier is the default behaviour in regular expressions, and it matches as much of the input as possible while still allowing the overall pattern to match. In other words, it tries to consume as many characters as possible and satisfy the entire regular expression.

Algorithm:

Step 1: Set the input string to be searched, for example, "abc123def456".

Step 2: Define a pattern using the Pattern class with a greedy quantifier for digits, i.e., "\\d+".

Step 3: Create a Matcher object using the input string and the defined greedy pattern.

Step 4: Loop through the input string to find and print matches:

  • Find the longest sequence of digits in the input string for each iteration.

Step 5: Print the matched group.

Implementation:

Filename: GreedyQuantifierExample.java

Output:

Match: 123
Match: 456

Reluctant Quantifier (Appending a ? after quantifier)

Reluctant quantifiers in Java regular expressions, also known as non-greedy or lazy quantifiers, match as little as possible, contrasting with greedy quantifiers. They match the first character initially and add more from the input string if needed. Appending a "?" to a greedy quantifier turns it into a reluctant quantifier. It is useful for extracting the shortest possible matching sequence in text processing tasks.

Algorithm:

Step 1: Set the input string to be searched, e.g., "abc123def456".

Step 2: Create a pattern using the Pattern class with a reluctant quantifier for digits, e.g., "\\d+?".

Step 3: Create a Matcher object using the input string and the reluctant pattern.

Step 4: Enter a loop to find and print matches in the input string using the matcher:

  • On each iteration, find the next Match in the input string.

Step 5: Display the matched group.

Implementation:

Filename: ReluctantQuantifierExample.java

Output:

Match: 1
Match: 2
Match: 3
Match: 4
Match: 5
Match: 6

Possessive Quantifier (Appending a + after quantifier)

A possessive quantifier in Java regular expressions is denoted by appending a plus sign + after a regular quantifier. The quantifier is similar to a greedy quantifier but with distinct behaviour. A possessive quantifier matches as much as possible and does not backtrack, even if doing so would allow the overall pattern to match.

Algorithm:

Step 1: Set the input string to be searched, e.g., "abcccc".

Step 2: Create a pattern using the Pattern class with a possessive quantifier, e.g., "c++".

Step 3: Create a Matcher object using the input string and the possessive pattern.

Step 4: Enter a loop to find and print matches in the input string using the matcher:

  • On each iteration, find the next Match in the input string.

Step 5: Display the matched group.

Implementation:

Filename: PossessiveQuantifierExample.java

Output:

Match: cccc






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA