@@ -5170,10 +5170,37 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
5170
5170
The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
5171
5171
can be used to force greediness or non-greediness, respectively,
5172
5172
on a subexpression or a whole RE.
5173
+ This is useful when you need the whole RE to have a greediness attribute
5174
+ different from what's deduced from its elements. As an example,
5175
+ suppose that we are trying to separate a string containing some digits
5176
+ into the digits and the parts before and after them. We might try to
5177
+ do that like this:
5178
+ <screen>
5179
+ SELECT regexp_matches('abc01234xyz', '(.*)(\d+)(.*)');
5180
+ <lineannotation>Result: </lineannotation><computeroutput>{abc0123,4,xyz}</computeroutput>
5181
+ </screen>
5182
+ That didn't work: the first <literal>.*</> is greedy so
5183
+ it <quote>eats</> as much as it can, leaving the <literal>\d+</> to
5184
+ match at the last possible place, the last digit. We might try to fix
5185
+ that by making it non-greedy:
5186
+ <screen>
5187
+ SELECT regexp_matches('abc01234xyz', '(.*?)(\d+)(.*)');
5188
+ <lineannotation>Result: </lineannotation><computeroutput>{abc,0,""}</computeroutput>
5189
+ </screen>
5190
+ That didn't work either, because now the RE as a whole is non-greedy
5191
+ and so it ends the overall match as soon as possible. We can get what
5192
+ we want by forcing the RE as a whole to be greedy:
5193
+ <screen>
5194
+ SELECT regexp_matches('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
5195
+ <lineannotation>Result: </lineannotation><computeroutput>{abc,01234,xyz}</computeroutput>
5196
+ </screen>
5197
+ Controlling the RE's overall greediness separately from its components'
5198
+ greediness allows great flexibility in handling variable-length patterns.
5173
5199
</para>
5174
5200
5175
5201
<para>
5176
- Match lengths are measured in characters, not collating elements.
5202
+ When deciding what is a longer or shorter match,
5203
+ match lengths are measured in characters, not collating elements.
5177
5204
An empty string is considered longer than no match at all.
5178
5205
For example:
5179
5206
<literal>bb*</>
0 commit comments