Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Quantifiers Of Regular Expression

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 537
    Comment on it

    The quantifiers describes the number of event of a character to match against. For ease of use we divide three types of quantifier:

    1. Greedy

    2. Reluctant

    3. Possessive

     

    1. Greedy quantifier:

    With the help of greedy quantifier matcher reads whole input string and attempting the first match. If the entered input string matches fail means it could not match to input string, the matcher backs off by one character and it will try again, It repeats the process until a match is found or it matches the whole input string till no more characters left to back off from. It is trying to match 1 or 0 characters.

    Eg: Enter the Regex: .*sri

         Enter input string to search: xsrixxxxxxsri

    I found the text "xsrixxxxxxsri" starting at index 0 and ending at index 13

    In the above example the greedy quantifier ‘.*’ to find anything, zero or more times, followed by the letters "s" "r" "i". Because the quantifier is greedy, the .* portion of the expression it takes the entire input string. In this example, the result of overall expression will false or fail, the reason behind this last three letters ("s" "r" "i") have already been used. so the matcher backs off one letter at a time until rightmost existence of the same letters"sri" has been found, at which point the match succeeds and the search ends.

     

    2. Reluctant quantifier:

        The reluctant quantifiers start at the beginning of the input string, then carefully take one character at one time and looking for a match.

    Eg:  Enter the regex: .*?sri
           Enter input string to search: xsrixxxsrisri
    I found the text "xsri" starting at index 0 and ending at index 4.

    I found the text "xxxsri" starting at index 4 and ending at index 10.

    I found the text "sri" starting at index 10 and ending at index 13.

    In the above example reluctant quantifier is .*?, starts by first consuming "nothing".

    The reason behind this ‘sri’ does not appear at the beginning of string, it’s forced to swallow the first letter i.e. x which triggers the first match 0 and 4, and second match will index 4 and 10 and another match at 10 and 13.

     

    3. Possessive quantifier:

    The possessive quantifiers take the whole input string for a match. Unlike the greedy quantifiers, possessive quantifiers never back off , even match will succeed.

    Eg: Enter your regex: .*+sri
          Enter input string to search: xsrixxxsrisri
          No match found.

    In the above example the possessive quantifier .*? take whole input string leaving nothing left over and it matches with the regex. The whole input string does not match with sri so the match is not immediately found.

    Below are the quantifier with description that allow to specify the number of occurrences to match:

    X?--> X occurs once or not at all

    X+--> X occurs once or more times

    X*--> X occurs zero or more times

    X{n}--> X occurs n times only

    X{n,}--> X occurs n or more times

    X{y,z}--> X occurs at least y times but less than z times

     

    import java.util.regex.*;
    class RegexExample{
    public static void main(String args[]){
    System.out.println(Pattern.matches("[ald]?", "a"));
    System.out.println(Pattern.matches("[adf]?", "aaa"));
    System.out.println(Pattern.matches("[ald]+", "a"));
    System.out.println(Pattern.matches("[ald]+", "aaa"));
    System.out.println(Pattern.matches("[ald]+", "aallldd"));
    System.out.println(Pattern.matches("[ald]+", "aazzta"));
    System.out.println(Pattern.matches("[ald]*", "alllda"));
    
    }
    }
    

    Output: true

                   false

                   true

                   true

                   true

                   false

                   true

    In the above example the first match is true because a or l or d comes one time.Second is false because a or l or d once or more times. Third is true because a or l or d once or more times. Fourth is true because a comes more than one time. Fifth is true because a or l or d comes more than once. Sixth is false because z and t are not matching pattern. Seventh is true because a or l or d may come zero or more times.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: