SQL RegEx

SQL RegEx : Quantifiers


Most of RegEx entities, we discussed so far, try to match only one occurrence of RegEx in Source Text.

The day has 200-105 question past delightfully. 200-105 question Delight itself, however, CISSP pdf is a weak term 300-208 questions CISSP pdf to express 300-208 questions the feelings of a naturalist who, for the first time, has been wandering by 200-105 question himself in a Brazilian 200-105 question CISSP pdf forest. Among the multitude of striking objects, the general luxuriance of the vegetation bears CISSP pdf away the victory. The CISSP pdf elegance of the grasses, the novelty of 300-208 questions the parasitical plants, the beauty of the flowers, the glossy 300-208 questions 300-208 questions green of 300-208 questions the foliage, CISSP pdf all tend to this 200-105 question end. 300-208 questions A most paradoxical mixture 300-208 questions of 200-105 question sound CISSP pdf and silence 200-105 question pervades the shady parts of the wood. 300-208 questions

The geology CISSP pdf of the surrounding country possesses little interest. Throughout the coast of Brazil, CISSP pdf and certainly for a considerable CISSP pdf space inland from the Rio Plata CISSP pdf CISSP pdf CISSP pdf to Cape St. 300-208 questions Roque, lat. 5S., 300-208 questions a distance of more than 2000 geographical 300-208 questions miles, wherever solid rock occurs, it CISSP pdf belongs to a granitic 300-208 questions formation. 300-208 questions 200-105 question The circumstance CISSP pdf 200-105 question of this enormous area being thus constituted of materials, which almost 200-105 question CISSP pdf every geologist believes have been crystallized by the action of heat under pressure, gives rise 200-105 question to many curious reflections. 200-105 question Was this effect produced 200-105 question beneath the depths of a profound ocean? Or did a covering of strata formerly extend over it, which has since been removed? Can we believe 200-105 question that 300-208 questions any power, 200-105 question action for 300-208 questions a time short of 200-105 question infinity, could have denuded the granite over so many thousand square leagues?

For example

RegEx :  [ab] :  This RegEx will try to find if source text contains ONE “a” or ONE “b”

RegEx : [a-bA-B0-9] :  This RegEx will try to find if source text contains any ONE of the characters defined by this character class.

Quantifiers, in RegEx, are used to verify more than one occurrences of string.

For example-

Source Text : “THIS is a example with a dummy text”

If we want to verify that source text contains exact 3 “a”s we can use a RegEx like-

RegEx : [a]{3}  : {3} is a quantifier to match exact 3 occurrences of preceding part of Regex

 

Source Text : 76430.98

If we want to verify that source text may or may not contain ONE decimal point, the RegEx would look like-

RegEx [\.]? : ? quantifier matches ZERO or ONE occurrence.

 

Types of Quantifiers

RegEx have 3 types of quantifiers as following-

Type of Quantifiers
Greedy Reluctant : ? Possessive : + Match X
X? X?? X?+ Zero or One time
X* X*? X*+ Zero or More times
X+ X+? X++ One or More times
X{n} X{n}? X{n}+ Exactly n times
X{n,} X{n,}? X{n,}+ n or more times (at least n times)
X{n,m} X{n,m}? X{n,m}+ at least n times but not more than m times

From this table, it may seem that all these types of quantifiers are doing the same thing i.e. trying to match given number of occurrences; but there is a major difference in the way these quantifiers process the source text and that directly impacts how they calculate number of occurrences.

1. Greedy quantifiers:

Greedy ones try to consume maximum text at once and as soon as match is found, it will stop looking for more matches.

Source Text : xfooxxxxxfoo

RegEx : .*foo 

  1. “.”  : can match any character
  2. “*” : can match one or more times
  3. foo : this is a string literal so it must be matched as it is

So this RegEx can match a text  with contains any number of any type of character followed by string “foo”.

In RegEx “.*foo   ==> “.*” is Greedy quantifier.

Here is how Greedy quantifiers work

  1. Greedy quantifiers will try to consume the complete source text for itself
    1. So, it tries to match text “xfooxxxxxfoo” with RegEx .*  ==> MATCH PASSED
    2. Now, it will try to match remaining part of RegEx (which is string foo) with remaining part of source text
      1. But complete source text is consumed by  “Greedy Part”, so there is no part of source text to match with  string foo ==> MATCH FAIL
  2. Because of Failed Match, in 1st step, Greedy quantifier will back off by 1 character from right
    1. So, it tries to match Text “xfooxxxxxfo”  with RegEx .*  ==> MATCH PASSED
    2. Now again, it will try to match remaining part of RegEx (which is string foo) with remaining part of source text
      1. Remaining part of source text is only one character “o”. It has not match with  string foo ==> MATCH FAIL
  3. Because of Failed Match in 2nd step, Greedy quantifier will back off by 1 character again
    1. So it tries to match Text “xfooxxxxxf”  with RegEx .*  ==> MATCH PASSED
    2. Now again, it will try to match remaining part of RegEx (which is string foo) with remaining part of source text
      1. Remaining part of source text are two characters “oo”. It has not match with  string foo ==> MATCH FAIL
  4. Because of Failed Match in 3rd step, Greedy quantifier will back off by 1 character again
    1. So, it tries to match Text “xfooxxxxx”  with RegEx .*  ==> MATCH PASSED
    2. Now again, it will try to match remaining part of RegEx (which is string foo) with remaining part of source text
      1. Remaining part of source string is “foo” which is exact match for string foo from RegEx  ==> MATCH PASSED.
  5. Since a match is found in 4th step, Greedy will not look back any more to find another possible match.
  6. If no match is found, it will keep moving back 1 character at a time till a match if found or it reached the 1st character of source text and there is no more characters left to move.

 

2. Reluctant quantifiers:

Reluctant quantifiers start from beginning of source text, and try to use minimum source text to find a match. After a match is found, it keeps looking for next match till complete source is not consumed.

Source Text : xfooxxxxxfoo

RegEx : .*?foo 

.*? part of the RegEx is reluctant quantifier (due to “?”)

Here is how it works-

  1. It starts from the beginning of source text and pick 1st character and try to match it with reluctant part
    1. 1st character from source “x” is matched with “.*” part of Regex ==> MATCH PASSED
    2. Now, it tries to check if next 3 character (“foo”) match to string foo (2nd part of RegEx) ==> MATCH PASSED
  2. Even after a match is found, this quantifier will keep looking for next match in remaining source text.
  3. It will found one more match in this source text
    1. .*      ==  “xxxxx”
    2. foo    ==  “foo”

 

3. Possessive quantifiers:

Possessive quantifiers are a bit similar to Greedy ones. Main difference between the two is – “Greedy ones keep backing off by one character until it finds a match; While, Possessive quantifiers do not back off to find a match; it just consumes the complete source text and if match is not found it does not try again.

Source Text : xfooxxxxxfoo

RegEx : .*+foo 

.*+ part of the RegEx is reluctant quantifier (due to “+”)

Here is how it works-

  1. Possessive quantifier will try to consume the complete source text for it self
    1. So, first it tries to match Text “xfooxxxxxfoo” with RegEx  .* + ==> MATCH PASSED
    2. Now it will try to match remaining part of RegEx (which is string foo) with remaining part of source text
      1. But complete source text is consumed by  possessive part, so there is no part of source text to match with  string foo ==> MATCH FAIL
  2. That is it. This quantifier makes only one attempt to find a match.
  3. I am still trying to find out a good case for these quantifiers.

IBM i developer.

View Comments
There are currently no comments.