

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# POSIX 运算符
<a name="pattern-matching-conditions-posix"></a>

POSIX 正则表达式是指定匹配模式的字符序列。如果字符串是正则表达式描述的正则集的成员，则该字符串与正则表达式匹配。

与 [LIKE](r_patternmatching_condition_like.md) 和 [SIMILAR TO](pattern-matching-conditions-similar-to.md) 运算符相比，POSIX 正则表达式提供了更强大的模式匹配手段。POSIX 正则表达式模式可匹配字符串的任何部分，这与 SIMILAR TO 运算符不同，SIMILAR TO 运算符仅当其模式匹配整个字符串时才返回 true。

**注意**  
使用 POSIX 运算符的正则表达式匹配的计算成本高昂。我们建议尽可能使用 LIKE，尤其是在处理非常多的行时。例如，下列查询的功能相同，但使用 LIKE 的查询相比于使用正则表达式的查询的运行速度快若干倍：  

```
select count(*) from event where eventname ~ '.*(Ring|Die).*'; 
select count(*) from event where eventname LIKE '%Ring%' OR eventname LIKE '%Die%';
```

## 语法
<a name="pattern-matching-conditions-posix-synopsis"></a>

```
expression [ ! ] ~ pattern
```

## 参数
<a name="pattern-matching-conditions-posix-arguments"></a>

 *expression*   
有效的 UTF-8 字符表达式（如列名称）。

\$1  
求反运算符。请不要与正则表达式匹配。

\$1  
对 *expression* 的任何子字符串执行区分大小写的匹配。  
`~~` 是 [LIKE](r_patternmatching_condition_like.md) 的同义词。

 * 模式*   
表示正则表达式模式的字符串文本。

如果 *pattern* 不包含通配符，则模式仅表示字符串本身。

要搜索包含元字符的字符串（如“`. * | ? `”等），请使用两个反斜杠（“` \\`”）对字符进行转义。与 `SIMILAR TO` 和 `LIKE` 不同，POSIX 正则表达式语法不支持用户定义的转义字符。

其中一个字符表达式可以是 CHAR 或 VARCHAR 数据类型。如果它们不同，Amazon Redshift 会将 *pattern* 转换为 *expression* 的数据类型。

所有字符表达式都可以是 CHAR 或 VARCHAR 数据类型。如果表达式的数据类型不同，Amazon Redshift 会将其转换为 *expression* 的数据类型。

POSIX 模式匹配支持下列元字符：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/pattern-matching-conditions-posix.html)

Amazon Redshift 支持下列 POSIX 字符类。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/pattern-matching-conditions-posix.html)

 Amazon Redshift 在正则表达式中支持下列受 Perl 影响的运算符。使用两个反斜杠（“`\\`”）转义此运算符。  

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/pattern-matching-conditions-posix.html)

## 示例
<a name="pattern-matching-conditions-posix-synopsis-examples"></a>

下表显示了使用 POSIX 运算符的模式匹配的示例：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/pattern-matching-conditions-posix.html)

以下示例查找名称包含 `E` 或 `H` 的城市：

```
SELECT DISTINCT city FROM users
WHERE city ~ '.*E.*|.*H.*' ORDER BY city LIMIT 5;

      city
-----------------
 Agoura Hills
 Auburn Hills
 Benton Harbor
 Beverly Hills
 Chicago Heights
```

以下示例查找名称不包含 `E` 或 `H` 的城市：

```
SELECT DISTINCT city FROM users WHERE city !~ '.*E.*|.*H.*' ORDER BY city LIMIT 5;

      city
-----------------
 Aberdeen	
 Abilene	
 Ada	
 Agat	
 Agawam
```

以下示例使用转义字符串（“`\\`”）搜索包含句点的字符串。

```
SELECT venuename FROM venue
WHERE venuename ~ '.*\\..*'
ORDER BY venueid;

          venuename
------------------------------
 St. Pete Times Forum
 Jobing.com Arena
 Hubert H. Humphrey Metrodome
 U.S. Cellular Field
 Superpages.com Center
 E.J. Nutter Center
 Bernard B. Jacobs Theatre
 St. James Theatre
```