Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • String Literals and Character Classes of regular expression

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 312
    Comment on it

    String Literals:

    The string literals is the most basic form of pattern matching of regular-expression.

    For example if regular-expression is srinivas and input string is srinivas then match will succeeds.

    Enter the regex: srinivas

    Enter input string to search: srinivas

    I found the text srinivas starting at index 0 and ending at index 8.

    This match was successful. Here input string is 8 character long and start
    index is 0 and end index is 8 as shown in the figure.

    In the above example string literal ‘srinivas’ ,each character in the string occupies in its own cells with index. The string 'srinivas' starts at index 0 and ends with 8 and characters are occupied 0,1,2,3,4,5,6,7,8 cells.

    In the above diagram the start index for the next match is same and end index is same as previous match.

    Enter the regex: sri

    Enter input string to search: srisrisri

    Found the text sri starting at index 0 and ending at index 3.

    Found the text sri starting at index 3 and ending at index 6.

    Found the text sri starting at index 6 and ending at index 9.

     

    In above example, regex is sri and input string is srisrisri, so it will index the input string and matches with the regex which will be found at start index 0 and at end index 3 in first match. In second match it will be found at start index 3 and at end index 6. In third match it will be found at start index 6 and at end index 9.

     

    Character classes:

    Below are the character classes with definition which support the regular-expression construct.

    1. [abc]--> a, b, or c (simple class)

    2. [^abc]--> Any character except a, b, or c (negation)

    3. [a-zA-Z]--> a through z or A through Z, inclusive (range)

    4. [ad[m-p]]--> a through d, or m through p: [a-dm-p] (union)

    5. [a-z&&[def]]--> d, e, or f (intersection)

    6. [a-z&&[^bc]]--> a through z, except for b and c: [ad-z] (subtraction)

    7. [a-z&&[^m-p]]--> a through z, and not m through p: [a-lq-z](subtraction)

     

    import java.util.regex.*;
    
    class RegexExample{
    public static void main(String args[]){
    System.out.println(Pattern.matches("[acdm]", "abcd"));
    System.out.println(Pattern.matches("[adl]", "a"));
    System.out.println(Pattern.matches("[alb]", "alllba"));
    }
    
    }
    

    Output:

    false

    true

    false

    In above example first output is false because here regex is [acdm] and input is a followed by b followed by c followed by d while it will match either a or c or d or m only. In second output is true because here regex is [adl] and input is a so match performs succeed and the third output is false because here regex is [alb] and input is a followed by l followed by l followed by l followed by b followed by a so a and l comes more than once.

 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: