【问题标题】:Get key value of matching case insensitive string获取匹配不区分大小写字符串的键值
【发布时间】:2020-05-15 02:43:17
【问题描述】:

我正在尝试从“id”字段中提取一些特定数据,但 jq 匹配区分大小写并导致搜索出现问题(基本上不匹配,因此返回 0 个结果)。

JSON 示例:

{
  "created_at": "2020-01-17T12:54:02Z",
  "primary": true,
  "verified": true,
  "deliverable_state": "deliverable",
  "undeliverable_count": 0,
  "updated_at": "2020-01-17T12:54:03Z",
  "url": "http://www.website.com",
  "id": 376062709553,
  "user_id": 374002305374,
  "type": "email",
  "value": "test-1234567@domain.com"
}
{
  "created_at": "2019-02-07T20:49:41Z",
  "primary": false,
  "verified": true,
  "deliverable_state": "deliverable",
  "undeliverable_count": 0,
  "updated_at": "2019-02-07T20:49:41Z",
  "url": "http://www.website.com",
  "id": 366235941554,
  "user_id": 374002305374,
  "type": "email",
  "value": "MyEmail@domain.com"
}

当针对以下情况运行 jq 时,我得到了正确的返回:

$ jq -r '. | select(.value=="MyEmail@domain.com") .id' sample.json
366235941554

但是如果我运行不正确的情况,例如。

$ jq -r '. | select(.value=="Myemail@domain.com") .id' sample.json

...我没有得到结果。我已经阅读了一些文档,但不幸的是,我不理解用于不敏感搜索的测试/匹配/捕获标志 (https://stedolan.github.io/jq/manual/#RegularexpressionsPCRE) 或如何让它与这个“定位并给我价值”请求一起运行。

我似乎到处都搜索过,但找不到任何使用此方法的示例。

【问题讨论】:

    标签: json jq


    【解决方案1】:

    你真的不需要正则表达式。

    select(.value | ascii_downcase == "myemail@domain.com") .id
    

    但如果您坚持,下面是您如何使用test/2 执行不区分大小写的匹配。

    select(.value | test("MyEmail@domain.com"; "i")) .id
    

    【讨论】:

      猜你喜欢
      • 2014-09-07
      • 2013-05-03
      • 2014-10-26
      • 2017-12-09
      • 2018-04-28
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多