Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Gpdb
提交
0471cd5f
G
Gpdb
项目概览
Greenplum
/
Gpdb
通知
7
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gpdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0471cd5f
编写于
1月 09, 2005
作者:
T
Tom Lane
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clarify description of greedy and non-greedy POSIX regular expressions,
per discussion in Nov 2004 with Ken Tanzer.
上级
a9566ccc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
94 addition
and
30 deletion
+94
-30
doc/src/sgml/func.sgml
doc/src/sgml/func.sgml
+94
-30
未找到文件。
doc/src/sgml/func.sgml
浏览文件 @
0471cd5f
<!--
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.23
3 2005/01/08 05:19:18
tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.23
4 2005/01/09 20:08:50
tgl Exp $
PostgreSQL documentation
-->
...
...
@@ -3772,45 +3772,109 @@ substring('foobar' from 'o(.)b') <lineannotation>o</lineannotation>
In the event that an RE could match more than one substring of a given
string, the RE matches the one starting earliest in the string.
If the RE could match more than one substring starting at that point,
its choice is determined by its <firstterm>preference</>:
either the longest substring, or the shortest.
either the longest possible match or the shortest possible match will
be taken, depending on whether the RE is <firstterm>greedy</> or
<firstterm>non-greedy</>.
</para>
<para>
Most atoms, and all constraints, have no preference.
A parenthesized RE has the same preference (possibly none) as the RE.
A quantified atom with quantifier
<literal>{</><replaceable>m</><literal>}</>
or
<literal>{</><replaceable>m</><literal>}?</>
has the same preference (possibly none) as the atom itself.
A quantified atom with other normal quantifiers (including
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</>
with <replaceable>m</> equal to <replaceable>n</>)
prefers longest match.
A quantified atom with other non-greedy quantifiers (including
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</>
with <replaceable>m</> equal to <replaceable>n</>)
prefers shortest match.
A branch has the same preference as the first quantified atom in it
which has a preference.
An RE consisting of two or more branches connected by the
<literal>|</> operator prefers longest match.
Whether an RE is greedy or not is determined by the following rules:
<itemizedlist>
<listitem>
<para>
Most atoms, and all constraints, have no greediness attribute (because
they cannot match variable amounts of text anyway).
</para>
</listitem>
<listitem>
<para>
Adding parentheses around an RE does not change its greediness.
</para>
</listitem>
<listitem>
<para>
A quantified atom with a fixed-repetition quantifier
(<literal>{</><replaceable>m</><literal>}</>
or
<literal>{</><replaceable>m</><literal>}?</>)
has the same greediness (possibly none) as the atom itself.
</para>
</listitem>
<listitem>
<para>
A quantified atom with other normal quantifiers (including
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</>
with <replaceable>m</> equal to <replaceable>n</>)
is greedy (prefers longest match).
</para>
</listitem>
<listitem>
<para>
A quantified atom with a non-greedy quantifier (including
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</>
with <replaceable>m</> equal to <replaceable>n</>)
is non-greedy (prefers shortest match).
</para>
</listitem>
<listitem>
<para>
A branch — that is, an RE that has no top-level
<literal>|</> operator — has the same greediness as the first
quantified atom in it that has a greediness attribute.
</para>
</listitem>
<listitem>
<para>
An RE consisting of two or more branches connected by the
<literal>|</> operator is always greedy.
</para>
</listitem>
</itemizedlist>
</para>
<para>
The above rules associate greediness attributes not only with individual
quantified atoms, but with branches and entire REs that contain quantified
atoms. What that means is that the matching is done in such a way that
the branch, or whole RE, matches the longest or shortest possible
substring <emphasis>as a whole</>. Once the length of the entire match
is determined, the part of it that matches any particular subexpression
is determined on the basis of the greediness attribute of that
subexpression, with subexpressions starting earlier in the RE taking
priority over ones starting later.
</para>
<para>
An example of what this means:
<screen>
SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');
<lineannotation>Result: </lineannotation><computeroutput>123</computeroutput>
SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
In the first case, the RE as a whole is greedy because <literal>Y*</>
is greedy. It can match beginning at the <literal>Y</>, and it matches
the longest possible string starting there, i.e., <literal>Y123</>.
The output is the parenthesized part of that, or <literal>123</>.
In the second case, the RE as a whole is non-greedy because <literal>Y*?</>
is non-greedy. It can match beginning at the <literal>Y</>, and it matches
the shortest possible string starting there, i.e., <literal>Y1</>.
The subexpression <literal>[0-9]{1,3}</> is greedy but it cannot change
the decision as to the overall match length; so it is forced to match
just <literal>1</>.
</para>
<para>
Subject to the constraints imposed by the rules for matching the whole RE,
subexpressions also match the longest or shortest possible substrings,
based on their preferences,
with subexpressions starting earlier in the RE taking priority over
ones starting later.
Note that outer subexpressions thus take priority over
their component subexpressions.
In short, when an RE contains both greedy and non-greedy subexpressions,
the total match length is either as long as possible or as short as
possible, according to the attribute assigned to the whole RE. The
attributes assigned to the subexpressions only affect how much of that
match they are allowed to <quote>eat</> relative to each other.
</para>
<para>
The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
can be used to force
longest and shortest preference
, respectively,
can be used to force
greediness or non-greediness
, respectively,
on a subexpression or a whole RE.
</para>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录