SQL RegEx : Capturing groups 102

Capturing Groups and Back-references

  1. Within RegEx, capturing groups can be used in the same way we use variables in programming languages.
    1. 1st define a variable, assign a value to that variable, and then value of the same variable can be used again.
  2. When RegEx is processed , if it finds a match for a “GROUP”, it saves the value of that GROUP in memory.
    1. Which can be referenced later in the RegEx using the GROUP NUMBER/NAME.
    2. This is called Back-reference.

Source Text : “this is a good example of a Back-Reference.”

For example, if  we want to find repeating words in this source text; we start from the left, pick a word, and check if picked word is the exact same or different.

Explanation of a RegEx Expression-(\b\w+)\s\1 is as following-

  1. (\b\w+) : Group 1 => start picking a word from beginning.
  2. \1 : This is a back reference to Group 1 .When RegEx control comes to this part, it just goes back to value of Group 1 and tries to match the exact same value again.
    1. So when it comes to the word “the”-
      1. Group 1 = “the”
    2. Followed by 1 space “\s”
    3. Due to “\1″, RegEx will look back to value of Group 1 (which is the word “the”)
      1. So the expression tries to match it with “the” again.
    4. so, the string “the the” is a match


Non Capturing Groups

  1. Let us say,  we want to create a group but do not want to capture it which means-
    1. we do not want to assign a GROUP number to that group
    2. we do not want to get value of that GROUP
    3. we do not want to back reference to that GROUP
  2. It is like, we just want to make our RegEx more readable by adding few more parenthesis but we do not want the system to save the value of this group.

How to define a Non Capturing Group?

(abc) ==> this  is a Capturing Group

(?:abc) ==> this is a Non Capturing Group.

  1. “?:” these 2 character (excluding quotes) at the starting of group makes it a Non Capturing Group.

Leave a Reply

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