

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

# Amazon SNS 订阅筛选策略
<a name="sns-subscription-filter-policies"></a>

订阅筛选策略允许您指定属性名称并向每个属性名称分配一个值列表。有关更多信息，请参阅 [Amazon SNS 消息筛选](sns-message-filtering.md)。

当 Amazon SNS 根据订阅筛选策略评估消息属性或消息正文属性时，它会忽略未在策略中指定的内容。

**重要**  
AWS 诸如 IAM 和 Amazon SNS 之类的服务使用一种称为最终一致性的分布式计算模型。对订阅筛选器策略的添加或更改最多需要 15 分钟即可完全生效。

在以下条件下，一个订阅接受一条消息：
+ 当筛选策略范围设置为 `MessageAttributes` 时，筛选策略中的每个属性名称都与消息属性名称相匹配。对于筛选策略中匹配的每个属性名称，至少有一个属性值与消息属性值相匹配。
+ 当筛选策略范围设置为 `MessageBody` 时，筛选策略中的每个属性名称都与消息正文属性名称相匹配。对于筛选策略中匹配的每个属性名称，至少有一个属性值与消息正文属性值相匹配。

Amazon SNS 目前支持以下筛选运算符：
+ [AND 逻辑](and-or-logic.md#and-logic)
+ [OR 逻辑](and-or-logic.md#or-logic)
+ [OR 运算符](and-or-logic.md#or-operator)
+ [键匹配](attribute-key-matching.md)
+ [数值精确匹配](numeric-value-matching.md#numeric-exact-matching)
+ [数值 anything-but 匹配](numeric-value-matching.md#numeric-anything-but-matching)
+ [数值范围匹配](numeric-value-matching.md#numeric-value-range-matching)
+ [字符串值精确匹配](string-value-matching.md#string-exact-matching)
+ [字符串值 anything-but 匹配](string-value-matching.md#string-anything-but-matching)
+ [使用前缀和 anything-but 运算符的字符串匹配](string-value-matching.md#string-anything-but-matching)
+ [字符串值等于-忽略大小写](string-value-matching.md#string-equals-ignore)
+ [字符串值 IP 地址匹配](string-value-matching.md#string-address-matching)
+ [字符串值前缀匹配](string-value-matching.md#string-prefix-matching)
+ [字符串值后缀匹配](string-value-matching.md#string-suffix-matching)

# Amazon SNS 筛选策略示例
<a name="example-filter-policies"></a>

下面的示例演示处理客户事务的 Amazon SNS 主题所发送的消息有效负载。

第一个示例包括 `MessageAttributes` 字段，其中包含描述事务的属性：
+ 客户的兴趣
+ 存储名称
+ 事件状态
+ 购买价格 (USD)

由于此消息包含 `MessageAttributes` 字段，只要在订阅中将 `FilterPolicyScope` 设置为 `MessageAttributes`，任何设置了 `FilterPolicy` 的主题订阅都可以选择性地接受或拒绝消息。有关将属性应用于消息的信息，请参阅 [Amazon SNS 消息属性](sns-message-attributes.md)。

```
{
   "Type": "Notification",
   "MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8",
   "TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic",
   "Message": "message-body-with-transaction-details",
   "Timestamp": "2019-11-03T23:28:01.631Z",
   "SignatureVersion": "4",
   "Signature": "signature",
   "UnsubscribeURL": "unsubscribe-url",
   "MessageAttributes": {
      "customer_interests": {
         "Type": "String.Array",
         "Value": "[\"soccer\", \"rugby\", \"hockey\"]"
      },
      "store": {
         "Type": "String",
         "Value":"example_corp"
      },
      "event": {
         "Type": "String",
         "Value": "order_placed"
      },
      "price_usd": {
         "Type": "Number", 
         "Value": "210.75"
      }
   }
}
```

以下示例显示了 `Message` 字段中包含的相同属性，也称为*消息有效负载*或*消息正文*。只要在订阅中将 `MessageBody` 设置为 `FilterPolicyScope`，任何设置了 `FilterPolicy` 的主题订阅都可以选择性地接受或拒绝消息。

```
{
"Type": "Notification",
   "MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8",
   "TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic",
   "Message": "{
      \"customer_interests\": [\"soccer\", \"rugby\", \"hockey\"],
      \"store\": \"example_corp\",
      \"event\":\"order_placed\",
      \"price_usd\":210.75
   }",
   "Timestamp": "2019-11-03T23:28:01.631Z",
   "SignatureVersion": "4",
   "Signature": "signature",
   "UnsubscribeURL": "unsubscribe-url"
}
```

以下筛选策略基于消息的属性名称和值接受或拒绝消息。

## 接受示例消息的策略
<a name="policy-accepts-messages"></a>

以下订阅筛选策略中的属性与分配给示例消息的属性匹配。请注意，无论设置为 `MessageAttributes` 还是 `MessageBody`，相同的筛选策略都适用于 `FilterPolicyScope`。每个订阅者根据他们从主题收到的消息的构成来选择其筛选范围。

如果此策略中的任一属性与分配给该消息的属性不匹配，则此策略将拒绝该消息。

```
{
   "store": ["example_corp"],
   "event": [{"anything-but": "order_cancelled"}],
   "customer_interests": [
      "rugby",
      "football",
      "baseball"
   ],
   "price_usd": [{"numeric": [">=", 100]}]
}
```

## 拒绝示例消息的策略
<a name="policy-rejects-messages"></a>

以下订阅筛选策略在其属性与分配给示例消息的属性之间存在多个不匹配项。例如，由于消息属性中不存在 `encrypted` 属性名称，因此该策略属性会导致消息被拒绝，而不管分配给它的值如何。

如果存在任何不匹配项，则策略将拒绝消息。

```
{
   "store": ["example_corp"],
   "event": ["order_cancelled"],
   "encrypted": [false],
   "customer_interests": [
      "basketball",
      "baseball"
   ]
}
```

# Amazon SNS 中的筛选策略限制
<a name="subscription-filter-policy-constraints"></a>

在 Amazon SNS 中设置筛选策略时，需要记住几个重要的规则。这些规则有助于确保筛选策略的有效应用，同时保持系统性能和兼容性。

## 常见策略限制
<a name="subscription-filter-policy-common-constraints"></a>

在 Amazon SNS 中配置筛选策略时，请遵循以下重要规则，确保策略有效应用的同时保持系统性能和兼容性：
+ **字符串匹配**：对于筛选策略中的字符串匹配，比较操作会区分大小写。
+ **数字匹配**：对于数字匹配，值的范围可以介于 -109 到 109（-10 亿到 10 亿）之间，小数点后精度为五位。
+ **筛选策略复杂度** – 筛选策略中的值的总和不得超过 **150**。要计算总的组合数，请将每个数组内所含值的个数相乘。
+ **限制密钥数量**：一个筛选策略最多可以有**五**个密钥。

****其它注意事项****
+ 筛选策略的 JSON 可包含：
  + 用引号引起来的字符串
  + 数字
  + 不带引号的关键字 `true`、`false` 和 `null`
+ 在使用 Amazon SNS API 时，必须将筛选策略的 JSON 作为有效的 **UTF-8** 字符串传递。
+ 筛选策略的最大大小为 **256 KB**。
+ 默认情况下，每个主题最多可以有 **200 个**筛选策略，每个 AWS 账户最多可以有 **10,000** 个筛选策略。

  此策略限制不会阻止使用 [https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html](https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html) API 创建 Amazon SQS 队列订阅。但是，当您在 `Subscribe` API 调用（或 [https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html](https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html) API 调用）中附加筛选策略时，操作将失败。

  要增加此限额，您可以使用 [AWS 服务限额](https://docs.aws.amazon.com/servicequotas/latest/userguide/intro.html)。

## 基于属性的筛选的策略限制
<a name="subscription-filter-policy-attribute-constraints"></a>

基于属性的筛选是默认选项。[https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html#API_SetSubscriptionAttributes_RequestParameters](https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html#API_SetSubscriptionAttributes_RequestParameters) 在订阅中设置为 `MessageAttributes`。
+ Amazon SNS 不接受基于属性的筛选的嵌套筛选策略。
+ Amazon SNS 仅将策略属性与具有以下数据类型的消息属性进行比较：
  + `String`
  + `String.Array`
**重要**  
在 Amazon SNS 中使用基于属性的筛选时，必须对某些特殊字符使用双重转义，特别是：  
双引号 (")
反斜杠 ()
未能对这些字符进行双重转义将导致筛选策略与已发布消息的属性不匹配，并且通知将无法传送。

**其它注意事项**
+ 不建议在数组中传递对象，因为基于属性的筛选不支持嵌套，可能会产生意想不到的结果。对嵌套策略使用基于有效载荷的筛选。
+ `Number` 支持数字属性值。
+ Amazon SNS 忽略具有二进制数据类型的消息属性。

**复杂度相关的示例策略：**

在以下策略示例中，第一个密钥有**三个**匹配运算符，第二个密钥有**一个**匹配运算符，第三个密钥有**两个**匹配运算符。

```
{
   "key_a": ["value_one", "value_two", "value_three"],
   "key_b": ["value_one"],
   "key_c": ["value_one", "value_two"]
}
```

总和是根据筛选策略中每个密钥的匹配运算符数量的乘积计算得出的：

```
3(match operators of key_a) 
x 1(match operators of key_b) 
x 2(match operators of key_c) 
= 6
```

## 基于有效负载的筛选的策略限制
<a name="subscription-filter-policy-payload-constraints"></a>

要从基于属性（默认）的筛选切换到基于有效载荷的筛选，您必须在订阅中将 [https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DeprovisionIpamPoolCidr.html](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DeprovisionIpamPoolCidr.html) 设置为 `MessageBody`。
+ Amazon SNS 接受基于有效负载的筛选的嵌套筛选策略。
+ 对于嵌套策略，只有**叶密钥**计入**五个**密钥的限制。

**密钥限制策略示例：**

在下方示例策略中：
+ 有两个叶密钥：`key_c` 和 `key_e`。
+ `key_c` 有**四个**匹配运算符，嵌套级别为**三**，`key_e` 有**三个**匹配运算符，嵌套级别为**二**。

```
{
"key_a": {
    "key_b": {
        "key_c": ["value_one", "value_two", "value_three", "value_four"]
        }
    },
"key_d": {
    "key_e": ["value_one", "value_two", "value_three"]
    }
}
```

总和是根据筛选策略中每个密钥的匹配运算符数量和每个密钥的嵌套层级的乘积计算得出的：

```
4(match operators of key_c) 
x 3(nested level of key_c) 
x 3(match operators of key_e) 
x 2(nested level of key_e) 
= 72
```

## 通配符模式使用准则
<a name="filter-policy-constraints-wildcard"></a>

 当您注册包含通配符的筛选策略时，Amazon SQS 会实施保护，以确保不会创建过于复杂的筛选策略，因为这会影响您的应用程序性能。

**模式结构**

字段包含一个或多个模式。以下示例显示了使用两种模式的字段：

```
{
    "greeting": [
      {"anything-but": {"prefix": "Hello"}},
      {"wildcard": "H*"}
    ] // 2 patterns
  }
```

**复杂度规则**
+  所有字段的通配符复杂度总和不得超过 100 个点 
+  每个模式最多 3 个通配符 

**复杂度计算**
+  字段复杂度 = `(Sum of pattern points)` × `(Number of patterns)` 
+ 模式点数：

   单通配符：1 个点 

   多个通配符：每个通配符 3 个点 

   Anything-but：1 个点 

  以下是复杂度计算的示例：

  ```
  {
    "filename": [
      {"wildcard": "*.txt"},     // 1 point
      {"wildcard": "log*"}      // 1 point
    ]                           // Total: (1 + 1) × 2 = 4 points
  }
  ```

# AND/OR 逻辑
<a name="and-or-logic"></a>

使用筛选策略中的 AND/OR 逻辑来匹配 Amazon SNS 中的消息属性或消息正文属性。这可以实现更精确、更灵活的消息筛选。

## AND 逻辑
<a name="and-logic"></a>

您可使用多个属性名称来应用 AND 逻辑。

考虑以下策略：

```
{
  "customer_interests": ["rugby"],
  "price_usd": [{"numeric": [">", 100]}]
}
```

它匹配任何 `customer_interests` 值设置为 `rugby` *且* `price_usd` 值设置为大于 100 的数字的消息属性或消息正文属性。

**注意**  
您无法将 AND 逻辑应用于同一属性的值。

## OR 逻辑
<a name="or-logic"></a>

通过将多个值分配给一个属性名称来应用 OR 逻辑。

考虑以下策略：

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

它匹配任何 `customer_interests` 值设置为 `rugby`、`football`、*或* `baseball` 的消息属性或消息正文属性。

## OR 运算符
<a name="or-operator"></a>

您可以使用 `"$or"` 运算符明确定义筛选策略，以表达策略中多个属性之间的 OR 关系。

只有当策略满足以下所有条件时，Amazon SNS 才会识别 `"$or"` 关系。当其中任一条件未满足时，`"$or"` 会被视为常规属性名称，与策略中的任何其他字符串相同。
+ 规则中有一个 `"$or"` 字段属性，后跟一个数组，例如，`“$or” : []`。
+ `"$or"` 数组中至少有 2 个对象：`"$or": [{}, {}]`。
+ `"$or"` 数组中的所有对象都没有属于保留关键字的字段名。

否则，`"$or"` 将被视为普通属性名称，与策略中的其他字符串相同。

由于数字和前缀是保留关键字，因此以下策略不会被解析为 OR 关系。

```
{ 
   "$or": [ {"numeric" : 123}, {"prefix": "abc"} ] 
}
```

**`OR` 运算符示例**

标准 `OR`：

```
{
  "source": [ "aws.cloudwatch" ], 
  "$or": [
    { "metricName": [ "CPUUtilization" ] },
    { "namespace": [ "AWS/EC2" ] }
  ] 
}
```

此策略的筛选逻辑是：

```
"source" && ("metricName" || "namespace")
```

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

```
"source": {"Type": "String", "Value": "aws.cloudwatch"},
"metricName": {"Type": "String", "Value": "CPUUtilization"}
```

或者

```
"source": {"Type": "String", "Value": "aws.cloudwatch"},
"namespace": {"Type": "String", "Value": "AWS/EC2"}
```

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

```
{
    "source": "aws.cloudwatch",
    "metricName": "CPUUtilization"
}
```

或者

```
{
    "source": "aws.cloudwatch",
    "namespace": "AWS/EC2"
}
```

### 包括 `OR` 关系在内的策略限制
<a name="or-operator-constraints"></a>

考虑以下策略：

```
{
    "source": [ "aws.cloudwatch" ],
    "$or": [
      { "metricName": [ "CPUUtilization", "ReadLatency" ] },
      {
        "metricType": [ "MetricType" ] ,
        "$or" : [
          { "metricId": [ 1234, 4321 ] },
          { "spaceId": [ 1000, 2000, 3000 ] }
        ]
      }
    ]
  }
```

该策略的逻辑也可以简化为：

```
("source" AND "metricName") 
OR 
("source" AND "metricType" AND "metricId") 
OR 
("source" AND "metricType" AND "spaceId")
```

具有 OR 关系的策略的复杂度计算可以简化为每个 OR 语句的组合复杂度之和。

总组合数的计算方法如下所示：

```
(source * metricName) + (source * metricType * metricId) + (source * metricType * spaceId)
= (1 * 2) + (1 * 1 * 2) + (1 * 1 * 3) 
= 7
```

`source` 有一个值，`metricName` 有两个值，`metricType` 有一个值，`metricId` 有两个值，`spaceId` 有三个值。

考虑以下嵌套筛选策略：

```
{
    "$or": [
      { "metricName": [ "CPUUtilization", "ReadLatency" ] },
      { "namespace": [ "AWS/EC2", "AWS/ES" ] }
    ],
    "detail" : {
      "scope" : [ "Service" ],
      "$or": [
        { "source": [ "aws.cloudwatch" ] },
        { "type": [ "CloudWatch Alarm State Change"] }
      ]
    }
  }
```

该策略的逻辑可以简化为：

```
("metricName" AND ("detail"."scope" AND "detail"."source")
OR
("metricName" AND ("detail"."scope" AND "detail"."type")
OR
("namespace" AND ("detail"."scope" AND "detail"."source")
OR
("namespace" AND ("detail"."scope" AND "detail"."type")
```

组合总数的计算方法与非嵌套策略相同，只是我们需要考虑键的嵌套级别。

总组合数的计算方法如下所示：

```
(2 * 2 * 2) + (2 * 2 * 2) + (2 * 2 * 2) + (2 * 2 * 2) = 32
```

`metricName` 有两个值，`namespace` 有两个值，`scope` 是具有一个值的两级嵌套键，`source` 是具有一个值的两级嵌套键，`type` 是具有一个值的两级嵌套键。

# 键匹配
<a name="attribute-key-matching"></a>

在筛选策略中使用 `exists` 运算符根据特定属性是否存在来匹配传入的消息。
+ `exists` 仅适用于叶节点（结构中的最终属性）。
+ 它不适用于嵌套 JSON 结构中的中间节点。
+ 使用 `"exists": true` 匹配包含指定属性的传入消息。键值必须为非空值。

  例如，以下策略属性使用值为 `true` 的 `exists` 运算符：

  ```
  "store": [{"exists": true}]
  ```

  它匹配包含 `store` 属性键的任何消息属性列表，如下所示：

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

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

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

  但是，它不匹配*不含* `store` 属性键的任何消息属性列表，如下所示：

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

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

  ```
  {
      "customer_interests": ["baseball", "basketball"]
  }
  ```
+ 使用 `"exists": false` 匹配*不* 包含指定属性的传入消息。
**注意**  
`"exists": false` 仅在存在至少一个属性时才匹配。一组空的属性会导致筛选条件不匹配。

  例如，以下策略属性使用值为 `false` 的 `exists` 运算符：

  ```
  "store": [{"exists": false}]
  ```

  它*不* 匹配包含 `store` 属性键的任何消息属性列表，如下所示：

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

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

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

  但是，它匹配*不含* `store` 属性键的任何消息属性列表，如下所示：

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

  它还匹配以下消息正文：

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

# 数值匹配
<a name="numeric-value-matching"></a>

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

**注意**  
前缀仅支持*字符串* 匹配。

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

当策略属性值包含 `numeric` 关键字和 `=` 运算符时，它将匹配具有相同名称和相同数值的任何消息属性值或消息正文属性值。

请考虑以下策略属性：

```
"price_usd": [{"numeric": ["=",301.5]}]
```

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

```
"price_usd": {"Type": "Number", "Value": 301.5}
```

```
"price_usd": {"Type": "Number", "Value": 3.015e2}
```

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

```
{
   "price_usd": 301.5
}
```

```
{
   "price_usd": 3.015e2
}
```

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

当策略属性值包含关键字 `anything-but` 时，它会匹配*不* 包含任何策略属性值的任何消息属性值或消息正文属性值。

请考虑以下策略属性：

```
"price": [{"anything-but": [100, 500]}]
```

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

```
"price": {"Type": "Number", "Value": 101}
```

```
"price": {"Type": "Number", "Value": 100.1}
```

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

```
{
   "price": 101
}
```

```
{
   "price": 100.1
}
```

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

```
"price": {"Type": "Number.Array", "Value": "[100, 50]"}
```

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

```
{
   "price": [100, 50]
}
```

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

```
"price": {"Type": "Number", "Value": 100}
```

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

```
{
   "price": 100
}
```

## 值范围匹配
<a name="numeric-value-range-matching"></a>

除了 `=` 运算符之外，数值策略属性还可以包含以下运算符：`<`、`<=`、`>` 和 `>=`。

请考虑以下策略属性：

```
"price_usd": [{"numeric": ["<", 0]}]
```

它匹配任何具有负数值的消息属性或消息正文属性。

考虑另一个消息属性：

```
"price_usd": [{"numeric": [">", 0, "<=", 150]}]
```

它匹配任何具有正数（最大为 150）的消息属性或消息正文属性。

# 字符串值匹配
<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"] }
```