简体   繁体   English

使用 jq 提取数组中的部分字符串元素

[英]Extract part of the string elements in an array using jq

I'm trying to extract part of the string in elements of an array, and create a new array with these extractions.我正在尝试提取数组元素中的部分字符串,并使用这些提取创建一个新数组。

[
  "local/binaries/app-2.21.0.tar.gz",
  "local/binaries/app-2.20.0.tar.gz",
  "local/binaries/app-2.19.1.tar.gz",
  "local/binaries/app-2.19.0.tar.gz",
  "local/binaries/app-2.18.0.tar.gz"
]

Desired output所需 output

[
  "app-2.21.0",
  "app-2.20.0",
  "app-2.19.1",
  "app-2.19.0",
  "app-2.18.0"
]

You can use jq's capture function with regular expressions.您可以将 jq 的capture function 与正则表达式一起使用。

jq '[.[] | capture("(?<captured>app-[0-9]+\\.[0-9]+\\.[0-9]+)") | .[]]'

Try it out on jq playground .在 jq 操场上试一试

Documentation: https://stedolan.github.io/jq/manual/#RegularexpressionsPCRE文档: https://stedolan.github.io/jq/manual/#RegularexpressionsPCRE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM