模式匹配操作符
数据库提供了三种独立的实现模式匹配的方法:SQL LIKE操作符、SIMILAR TO操作符和POSIX-风格的正则表达式。除了这些基本的操作符外,还有一些函数可用于提取或替换匹配子串并在匹配位置分离一个串。
LIKE
描述:判断字符串是否能匹配上LIKE后的模式字符串。如果字符串与提供的模式匹配,则LIKE表达式返回为真(NOT LIKE表达式返回假),否则返回为假(NOT LIKE表达式返回真)。
匹配规则:
此操作符只有在它的模式匹配整个串的时候才能成功。如果要匹配在串内任何位置的序列,该模式必须以百分号开头和结尾。
下划线(_)代表(匹配)任何单个字符;百分号(%)代表任意串的通配符。
要匹配文本里的下划线或者百分号,在提供的模式里相应字符必须前导逃逸字符。逃逸字符的作用是禁用元字符的特殊含义,缺省的逃逸字符是反斜线,也可以用ESCAPE子句指定一个不同的逃逸字符。
要匹配逃逸字符本身,写两个逃逸字符。例如要写一个包含反斜线的模式常量,那你就要在SQL语句里写两个反斜线。
说明:
参数standard_conforming_strings设置为off时,在文串常量中写的任何反斜线都需要被双写。因此,写一个匹配单个反斜线的模式实际上要在语句里写四个反斜线(你可以通过用ESCAPE选择一个不同的逃逸字符来避免这种情况,这样反斜线就不再是LIKE的特殊字符了。但仍然是字符文本分析器的特殊字符,所以你还是需要两个反斜线)。在兼容MYSQL数据模式时,您也可以通过写ESCAPE ''的方式不选择逃逸字符,这样可以有效地禁用逃逸机制,但是没有办法关闭下划线和百分号在模式中的特殊含义。
关键字ILIKE可以用于替换LIKE,区别是LIKE大小写敏感,ILIKE大小写不敏感。
操作符~~等效于LIKE,操作符~~*等效于ILIKE。
示例:
openGauss=# SELECT 'abc' LIKE 'abc' AS RESULT;
result
-----------
t
(1 row)
openGauss=# SELECT 'abc' LIKE 'a%' AS RESULT;
result
-----------
t
(1 row)
openGauss=# SELECT 'abc' LIKE '_b_' AS RESULT;
result
-----------
t
(1 row)
openGauss=# SELECT 'abc' LIKE 'c' AS RESULT;
result
-----------
f
(1 row)
SIMILAR TO
描述:SIMILAR TO操作符根据自己的模式是否匹配给定串而返回真或者假。它和LIKE非常类似,只不过它使用SQL标准定义的正则表达式理解模式。
匹配规则:
1. 和LIKE一样,此操作符只有在它的模式匹配整个串的时候才能成功。如果要匹配在串内任何位置的序列,该模式必须以百分号开头和结尾。
2. 下划线(\_)代表(匹配)任何单个字符;百分号(%)代表任意串的通配符。
3. SIMILAR TO也支持下面这些从POSIX正则表达式借用的模式匹配元字符。
<a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_table199321611038"></a>
<table><thead align="left"><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row3083601711038"><th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.1"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6509621711038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6509621711038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6509621711038"></a>元字符</p>
</th>
<th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.2"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3830219211038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3830219211038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3830219211038"></a>含义</p>
</th>
</tr>
</thead>
<tbody><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row917541411038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p501109211038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p501109211038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p501109211038"></a>|</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p324528211038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p324528211038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p324528211038"></a>表示选择(两个候选之一)</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row2920753811038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1700039611038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1700039611038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1700039611038"></a>*</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3485482611038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3485482611038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3485482611038"></a>表示重复前面的项零次或更多次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row4525798211038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4201795011038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4201795011038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4201795011038"></a>+</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4801080711038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4801080711038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4801080711038"></a>表示重复前面的项一次或更多次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row2944408011038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3616024011038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3616024011038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3616024011038"></a>?</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4329830011038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4329830011038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4329830011038"></a>表示重复前面的项零次或一次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row5414038711038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2329521311038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2329521311038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2329521311038"></a>{m}</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p786412011038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p786412011038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p786412011038"></a>表示重复前面的项刚好m次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row366822111038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2869050811038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2869050811038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2869050811038"></a>{m,}</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4222980711038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4222980711038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4222980711038"></a>表示重复前面的项m次或更多次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row4452394811038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4967002511038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4967002511038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4967002511038"></a>{m,n}</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6384907011038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6384907011038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6384907011038"></a>表示重复前面的项至少m次并且不超过n次</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row3777072411038"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3952976911038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3952976911038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3952976911038"></a>()</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4779469811038"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4779469811038"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4779469811038"></a>把多个项组合成一个逻辑项</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row4859307511333"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4372496511333"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4372496511333"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4372496511333"></a>[...]</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5206129211333"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5206129211333"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5206129211333"></a>声明一个字符类,就像POSIX正则表达式一样</p>
</td>
</tr>
</tbody>
</table>
4. 前导逃逸字符可以禁止所有这些元字符的特殊含义。逃逸字符的使用规则和LIKE一样。
正则表达式函数:
支持使用函数[substring\(string from pa...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_la1fee63f0fe34c148a0649f508a3048c)截取匹配SQL正则表达式的子字符串。
示例:
```
openGauss=# SELECT 'abc' SIMILAR TO 'abc' AS RESULT;
result
-----------
t
(1 row)
```
```
openGauss=# SELECT 'abc' SIMILAR TO 'a' AS RESULT;
result
-----------
f
(1 row)
```
```
openGauss=# SELECT 'abc' SIMILAR TO '%(b|d)%' AS RESULT;
result
-----------
t
(1 row)
```
```
openGauss=# SELECT 'abc' SIMILAR TO '(b|c)%' AS RESULT;
result
-----------
f
(1 row)
```
POSIX正则表达式
描述:正则表达式是一个字符序列,它是定义一个串集合(一个正则集)的缩写。如果一个串是正则表达式描述的正则集中的一员时,我们就说这个串匹配该正则表达式。 POSIX正则表达式提供了比LIKE和SIMILAR TO操作符更强大的含义。[表1](#zh-cn_topic_0283137455_zh-cn_topic_0237121970_table6512684711360)列出了所有可用于POSIX正则表达式模式匹配的操作符。
**表 1** 正则表达式匹配操作符
<a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_table6512684711360"></a>
<table><thead align="left"><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row1082180211360"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3735685611360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3735685611360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3735685611360"></a>操作符</p>
</th>
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p600647111360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p600647111360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p600647111360"></a>描述</p>
</th>
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1676213111360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1676213111360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1676213111360"></a>例子</p>
</th>
</tr>
</thead>
<tbody><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row1664145111360"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p578025211360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p578025211360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p578025211360"></a>~</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6554726211360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6554726211360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6554726211360"></a>匹配正则表达式,大小写敏感</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p772799211360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p772799211360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p772799211360"></a>'thomas' ~ '.*thomas.*'</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row244307011360"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6367099211360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6367099211360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p6367099211360"></a>~*</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5707675111360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5707675111360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5707675111360"></a>匹配正则表达式,大小写不敏感</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5981411411360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5981411411360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5981411411360"></a>'thomas' ~* '.*Thomas.*'</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row145611911360"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5083680411360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5083680411360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p5083680411360"></a>!~</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2414044111360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2414044111360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p2414044111360"></a>不匹配正则表达式,大小写敏感</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p921871911360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p921871911360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p921871911360"></a>'thomas' !~ '.*Thomas.*'</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row1585961211360"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p956017311360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p956017311360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p956017311360"></a>!~*</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3617658011360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3617658011360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p3617658011360"></a>不匹配正则表达式,大小写不敏感</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4462183011360"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4462183011360"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p4462183011360"></a>'thomas' !~* '.*vadim.*'</p>
</td>
</tr>
</tbody>
</table>
匹配规则:
1. 与LIKE不同,正则表达式允许匹配串里的任何位置,除非该正则表达式显式地挂接在串的开头或者结尾。
2. 除了上文提到的元字符外, POSIX正则表达式还支持下列模式匹配元字符。
<a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_table23243447154559"></a>
<table><thead align="left"><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row10310639154559"><th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.1"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p263474154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p263474154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p263474154559"></a>元字符</p>
</th>
<th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.2"><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p21341433154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p21341433154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p21341433154559"></a>含义</p>
</th>
</tr>
</thead>
<tbody><tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row55757359154559"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p20052206154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p20052206154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p20052206154559"></a>^</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p13615978154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p13615978154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p13615978154559"></a>表示串开头的匹配</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row915516111321"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p171561119328"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p171561119328"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p171561119328"></a>$</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1841276153213"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1841276153213"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p1841276153213"></a>表示串末尾的匹配</p>
</td>
</tr>
<tr id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_row24758039154559"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p59244166154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p59244166154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p59244166154559"></a>.</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p34048137154559"><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p34048137154559"></a><a name="zh-cn_topic_0283137455_zh-cn_topic_0237121970_p34048137154559"></a>匹配任意单个字符</p>
</td>
</tr>
</tbody>
</table>
正则表达式函数:
POSIX正则表达式支持下面函数。
- [substring\(string from pa...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_leae425d0ea44492abccc5db3556aa6f4)函数提供了抽取一个匹配POSIX正则表达式模式的子串的方法。
- [regexp\_count\(string tex...](字符处理函数和操作符.md#li1459744717314)函数提供了获取匹配POSIX正则表达式模式的子串数量的功能。
- [regexp\_instr\(string tex...](字符处理函数和操作符.md#li11481132119326)函数提供了获取匹配POSIX正则表达式模式子串位置的功能。
- [regexp\_substr\(string te...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_l5385d9c47e9b4b6582d8fe6a87464190)函数提供了抽取一个匹配POSIX正则表达式模式的子串的方法。
- [regexp\_replace\(string, p...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_l3d209c16ba5f452798f2875e0144da4f)函数提供了将匹配POSIX正则表达式模式的子串替换为新文本的功能。
- [regexp\_matches\(string te...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_l780ff57395f849c39cd24ae7bbc09950)函数返回一个文本数组,该数组由匹配一个POSIX正则表达式模式得到的所有被捕获子串构成。
- [regexp\_split\_to\_table\(st...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_l75315d2c7397491e8c5b10466b3fff91)函数把一个POSIX正则表达式模式当作一个定界符来分离一个串。
- [regexp\_split\_to\_array\(st...](字符处理函数和操作符.md#zh-cn_topic_0283137513_zh-cn_topic_0237121967_zh-cn_topic_0059779223_l0af8a0d78ca24b35815a93c7305cafdd)和regexp\_split\_to\_table类似,是一个正则表达式分离函数,不过它的结果以一个text数组的形式返回。
> **说明:**
>
>正则表达式分离函数会忽略零长度的匹配,这种匹配发生在串的开头或结尾或者正好发生在前一个匹配之后。这和正则表达式匹配的严格定义是相悖的,后者由regexp\_matches实现,但是通常前者是实际中最常用的行为。
示例:
```
openGauss=# SELECT 'abc' ~ 'Abc' AS RESULT;
result
--------
f
(1 row)
```
```
openGauss=# SELECT 'abc' ~* 'Abc' AS RESULT;
result
--------
t
(1 row)
```
```
openGauss=# SELECT 'abc' !~ 'Abc' AS RESULT;
result
--------
t
(1 row)
```
```
openGauss=# SELECT 'abc'!~* 'Abc' AS RESULT;
result
--------
f
(1 row)
```
```
openGauss=# SELECT 'abc' ~ '^a' AS RESULT;
result
--------
t
(1 row)
```
```
openGauss=# SELECT 'abc' ~ '(b|d)'AS RESULT;
result
--------
t
(1 row)
```
```
openGauss=# SELECT 'abc' ~ '^(b|c)'AS RESULT;
result
--------
f
(1 row)
```
虽然大部分的正则表达式搜索都能很快地执行,但是正则表达式仍可能被人为地弄成需要任意长的时间和任意量的内存进行处理。不建议从非安全模式来源接受正则表达式搜索模式,如果必须这样做,建议加上语句超时限制。使用SIMILAR TO模式的搜索具有同样的安全性危险,因为SIMILAR TO提供了很多和POSIX-风格正则表达式相同的能力。LIKE搜索比其他两种选项简单得多,因此在接受非安全模式来源搜索时要更安全些。
特性补充:目前B库支持的字符序支持右模糊匹配支持索引扫描,会将匹配条件转换为大于等于和小于等于两个不等式作为索引条件。以一个c1 char(10)类型的列举例来说明索引条件转换规则。当我们的查询条件是where c1 like 'sdf%'时,会将索引条件转换为c1 >= 'sdf\min(7 times)' and c1<='sdf\max(7 times)'。其中\min与\max是每种字符序对应的最大排序的字符编码,这样的转换保证我们转换出的索引条件一定是与like 'sdf%'等价的。也是由于这样的转换规则,通过explain打印执行计划的时候,会导致显示不够友好,索引条件中会出现不可视字符,并且由于填充长度可能会很长,会使得执行计划长度较大。