

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 字符串值匹配
<a name="string-value-matching"></a>

通过将字符串值与消息属性值或消息正文属性值进行匹配来筛选消息。在 JSON 策略中，用双引号将字符串值引起来。您可以使用以下字符串操作来匹配消息属性或消息正文属性。

## 精确匹配
<a name="string-exact-matching"></a>

在策略属性值与一个或多个消息属性值匹配时，会进行精确匹配。对于 `String.Array` 类型属性，出于匹配目的，数组中的每个元素都被视为一个单独的字符串。

请考虑以下策略属性：

```
"customer_interests": ["rugby", "tennis"]
```

它匹配以下消息属性：

```
"customer_interests": {"Type": "String", "Value": "rugby"}
```

```
"customer_interests": {"Type": "String", "Value": "tennis"}
```

```
"customer_interests": {"Type": "String.Array", "Value": "[\"rugby\", \"tennis\"]"}
```

它还匹配以下消息正文：

```
{
   "customer_interests": "rugby"
}
```

```
{
   "customer_interests": "tennis"
}
```

但是，它不匹配以下消息属性：

```
"customer_interests": {"Type": "String", "Value": "baseball"}
```

```
"customer_interests": {"Type": "String.Array", "Value": "[\"baseball\"]"}
```

它与以下消息正文也不匹配：

```
{
   "customer_interests": "baseball"
}
```

## Anything-but 匹配
<a name="string-anything-but-matching"></a>

当策略属性值包含关键字 `anything-but` 时，它会匹配*不* 包含任何策略属性值的任何消息属性值或消息正文值。`anything-but` 可以与 `"exists": false` 组合。对于 `String.Array` 类型属性，如果策略属性中没有列出任何数组元素，则视为匹配。

请考虑以下策略属性：

```
"customer_interests": [{"anything-but": ["rugby", "tennis"]}]
```

它匹配以下任一消息属性：

```
"customer_interests": {"Type": "String", "Value": "baseball"}
```

```
"customer_interests": {"Type": "String", "Value": "football"}
```

```
"customer_interests": {"Type": "String.Array", "Value": "[\"rugby\", \"baseball\"]"}
```

它还匹配以下任一消息正文：

```
{
   "customer_interests": "baseball"
}
```

```
{
   "customer_interests": "football"
}
```

此外，它还匹配以下消息属性（因为它包含的值*不是* `rugby` 或 `tennis`）：

```
"customer_interests": {"Type": "String.Array", "Value": "[\"rugby\", \"baseball\"]"}
```

它还匹配以下消息正文（因为它包含的值不是 `rugby` 或 `tennis`）：

```
{
   "customer_interests": ["rugby", "baseball"]
}
```

但是，它不匹配以下消息属性：

```
"customer_interests": {"Type": "String", "Value": "rugby"}
```

```
"customer_interests": {"Type": "String.Array", "Value": "[\"rugby\"]"}
```

它与以下消息正文也不匹配：

```
{
   "customer_interests": ["rugby"]
}
```

**将前缀和 `anything-but` 结合使用**

要进行字符串匹配，您也可以将前缀与 `anything-but` 运算符结合使用。例如，以下策略属性拒绝 `order-` 前缀：

```
"event":[{"anything-but": {"prefix": "order-"}}]
```

它匹配以下任一属性：

```
"event": {"Type": "String", "Value": "data-entry"}
```

```
"event": {"Type": "String", "Value": "order_number"}
```

它还匹配以下任一消息正文：

```
{
   "event": "data-entry"
}
```

```
{
   "event": "order_number"
}
```

但是，它不匹配以下消息属性：

```
"event": {"Type": "String", "Value": "order-cancelled"}
```

它与以下消息正文也不匹配：

```
{
   "event": "order-cancelled"
}
```

**anything-but 通配符**

以下策略属性拒绝 `*ball` 通配符：

```
"customer_interests" : [{ "anything-but": { "wildcard": "*ball" }}]
```

它匹配以下属性：

```
{"customer_interests": ["hockey", "rugby", "soccer] }
```

但是，它不匹配以下消息属性：

```
{"customer_interests": ["baseball", "basketball"] }
```

**anything-but 前缀** 

以下策略属性拒绝 `-ball`

 后缀：

```
"customer_interests": [ { "anything-but": { "suffix": "ball" } } ]
```

它匹配以下属性：

```
{"customer_interests": ["hockey", "rugby", "soccer] }
```

但是，它不匹配以下消息属性：

```
 {"customer_interests": ["baseball", "basketball"] }
```

## Equals-ignore-case 匹配
<a name="string-equals-ignore"></a>

当策略属性包含关键字 `equals-ignore-case` 时，它将与任何信息属性或正文属性值进行不区分大小写的匹配。

请考虑以下策略属性：

```
"customer_interests": [{"equals-ignore-case": "tennis"}]
```

它匹配以下任一消息属性：

```
"customer_interests": {"Type": "String", "Value": "TENNIS"}
```

```
"customer_interests": {"Type": "String", "Value": "Tennis"}
```

它还匹配以下任一消息正文：

```
{
    "customer_interests": "TENNIS"
}
```

```
{
    "customer_interests": "teNnis"
{
```

## IP 地址匹配
<a name="string-address-matching"></a>

您可以使用 `cidr` 运算符来检查传入消息是否源自特定 IP 地址或子网。

请考虑以下策略属性：

```
"source_ip":[{"cidr": "10.0.0.0/24"}]
```

它匹配以下任一消息属性：

```
"source_ip": {"Type": "String", "Value": "10.0.0.0"}
```

```
"source_ip": {"Type": "String", "Value": "10.0.0.255"}
```

它还匹配以下任一消息正文：

```
{
   "source_ip": "10.0.0.0"
}
```

```
{
   "source_ip": "10.0.0.255"
}
```

但是，它不匹配以下消息属性：

```
"source_ip": {"Type": "String", "Value": "10.1.1.0"}
```

它与以下消息正文也不匹配：

```
{
   "source_ip": "10.1.1.0"
}
```

## 前缀匹配
<a name="string-prefix-matching"></a>

当策略属性值包含关键字 `prefix` 时，它匹配以指定字符开头的任何消息属性值或正文属性值。

请考虑以下策略属性：

```
"customer_interests": [{"prefix": "bas"}]
```

它匹配以下任一消息属性：

```
"customer_interests": {"Type": "String", "Value": "baseball"}
```

```
"customer_interests": {"Type": "String", "Value": "basketball"}
```

它还匹配以下任一消息正文：

```
{
   "customer_interests": "baseball"
}
```

```
{
   "customer_interests": "basketball"
}
```

但是，它不匹配以下消息属性：

```
"customer_interests": {"Type": "String", "Value": "rugby"}
```

它与以下消息正文也不匹配：

```
{
   "customer_interests": "rugby"
}
```

## 后缀匹配
<a name="string-suffix-matching"></a>

当策略属性值包含关键字 `suffix` 时，它匹配以指定字符结尾的任何消息属性值或正文属性值。

请考虑以下策略属性：

```
"customer_interests": [{"suffix": "ball"}]
```

它匹配以下任一消息属性：

```
"customer_interests": {"Type": "String", "Value": "baseball"}
```

```
"customer_interests": {"Type": "String", "Value": "basketball"}
```

它还匹配以下任一消息正文：

```
{
    "customer_interests": "baseball"
}
```

```
{
    "customer_interests": "basketball"
}
```

但是，它不匹配以下消息属性：

```
"customer_interests": {"Type": "String", "Value": "rugby"}
```

它与以下消息正文也不匹配：

```
{
    "customer_interests": "rugby"
}
```

## 通配符
<a name="string-value-wildcard"></a>

 您可以使用通配符 (\$1) 匹配事件模式中的字符串值。

以下策略使用通配符 (\$1) 字符：

```
"customer_interests": [ { "wildcard": "*ball" } ]
```

它匹配以下属性：

```
{"customer_interests": ["baseball", "basketball"] }
```