Skip to content

Commit aa9f8cb

Browse files
committed
Docs: add an explicit example about controlling overall greediness of REs.
Per discussion of bug #13538.
1 parent fa6e785 commit aa9f8cb

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5187,10 +5187,37 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
51875187
The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
51885188
can be used to force greediness or non-greediness, respectively,
51895189
on a subexpression or a whole RE.
5190+
This is useful when you need the whole RE to have a greediness attribute
5191+
different from what's deduced from its elements. As an example,
5192+
suppose that we are trying to separate a string containing some digits
5193+
into the digits and the parts before and after them. We might try to
5194+
do that like this:
5195+
<screen>
5196+
SELECT regexp_matches('abc01234xyz', '(.*)(\d+)(.*)');
5197+
<lineannotation>Result: </lineannotation><computeroutput>{abc0123,4,xyz}</computeroutput>
5198+
</screen>
5199+
That didn't work: the first <literal>.*</> is greedy so
5200+
it <quote>eats</> as much as it can, leaving the <literal>\d+</> to
5201+
match at the last possible place, the last digit. We might try to fix
5202+
that by making it non-greedy:
5203+
<screen>
5204+
SELECT regexp_matches('abc01234xyz', '(.*?)(\d+)(.*)');
5205+
<lineannotation>Result: </lineannotation><computeroutput>{abc,0,""}</computeroutput>
5206+
</screen>
5207+
That didn't work either, because now the RE as a whole is non-greedy
5208+
and so it ends the overall match as soon as possible. We can get what
5209+
we want by forcing the RE as a whole to be greedy:
5210+
<screen>
5211+
SELECT regexp_matches('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
5212+
<lineannotation>Result: </lineannotation><computeroutput>{abc,01234,xyz}</computeroutput>
5213+
</screen>
5214+
Controlling the RE's overall greediness separately from its components'
5215+
greediness allows great flexibility in handling variable-length patterns.
51905216
</para>
51915217

51925218
<para>
5193-
Match lengths are measured in characters, not collating elements.
5219+
When deciding what is a longer or shorter match,
5220+
match lengths are measured in characters, not collating elements.
51945221
An empty string is considered longer than no match at all.
51955222
For example:
51965223
<literal>bb*</>

0 commit comments

Comments
 (0)