Skip to content

Commit ea1703e

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

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
@@ -5170,10 +5170,37 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
51705170
The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
51715171
can be used to force greediness or non-greediness, respectively,
51725172
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.
51735199
</para>
51745200

51755201
<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.
51775204
An empty string is considered longer than no match at all.
51785205
For example:
51795206
<literal>bb*</>

0 commit comments

Comments
 (0)