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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 490
    Comment on it

    A backslash (\) followed by a digit indicates that the defined groups to be recalled is known as backreferences. For example the defined capturing group (\D\D) matching two consecutive non-digits which can be recalled with added ‘\1’ in capturing group means if regular expression is (\D\D)\1, It will match 2 consecutive non-digit character from capturing group and matches exact same two 2 consecutive non-digit character from ‘\1’. In some tool it accepts ‘\\’ instead of ‘\’.

     

    Eg: Enter the regex: (\\D\\D)\\1

           Enter the input string to search: s*s*

    I found the text “s*s*” starting at index 0 and ending at index 4.

    For using of backreferences in our regular expression should give input string followed by exact same input string according to capturing group or vice versa.

    Eg: Enter the regex: (\\d\\d\\d)\\1

           Enter the input string to search: 123123

    I found the text “123123” starting at index 0 and ending at index 6.

    Eg: Enter the regex: (\\D\\D)\\1

           Enter the input string to search: ab12

    No match found


    Boundary Matchers:

      We can make our pattern more accurate for matching against input string with the help of boundary matchers. For example if we are finding a word which appears at beginning or end of a line then we can use here a boundary matchers through which matching operation will be performed easily.

    The following below are the boundary matchers with description :

    Boundary matchers →    Description

    ^                                  → Beginning of a line

    $                                   → The end of a line

    \b                                  → A word boundary

    \B                                  → A non-word boundary

    \A                                  → The beginning of the input

    \G                                 → The end of the previous match

    \Z                                 → The end of the input for the final terminator, if any

    \z                                 → The end of the input

     

    Eg: Enter the regex: ^sri$

           Enter input string to search: ‘sri’

    I found the text ‘sri’ starting at index 0 and ending at index 3.

     

     Eg:    Enter your regex: ^sri$

               Enter input string to search:  ‘      sri’

    No match found.

     

    Eg:      Enter your regex: \s*sri$

                Enter input string to search: ‘            sri’

    I found the text ‘            sri’ starting at index 0 and ending at index 15.

     

     Eg:    Enter your regex: ^sri\w*

               Enter input string to search: ‘srinivasa’

    I found the text ‘srinivasa’ starting at index 0 and ending at index 9.

     

     Eg:   Enter the regex: \bsri\b

              Enter input string to search: His friend sri plays in the field

    I found the text ‘sri’ starting at index 11 and ending at index 14.

     

    Eg:  Enter the regex: \bsri\B

            Enter input string to search: His friend sri plays in the field

    No match found.

     

    The above first example is successful because the pattern hold entire input string, the second example fails or false because in the pattern start with the character but we are passing the input start with space, In the third example it is successful because the pattern hold unlimited whitespace beginning of the input string. In the fourth example it is successful because the pattern hold the unlimited word characters followed by input string, In the fifth example it is successful because the pattern hold the word boundary matches the input string while in sixth example it is unsuccessful because the pattern hold the word boundary starting of the string and ending with non-word boundary.

 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: