Jq: Escaping dot in regex does not work

Created on 8 Oct 2016  路  2Comments  路  Source: stedolan/jq

Hey there,

I'm trying to use jq's regex to sub semver, but I cannot escape the dot, as it throws a compiler error.

echo "{\"VERSION\": \"0.2.1-alpha+abxc23\"}" |\
jq '.VERSION | sub("(?<vers>[0-9]+\.[0-9]+\.[0-9]+).*"; (.vers))'

Should give me the version, but fails with a compilation error.

Most helpful comment

$ echo "{\"VERSION\": \"0.2.1-alpha+abxc23\"}" |\
  jq '.VERSION | sub("(?<vers>[0-9]+\\.[0-9]+\\.[0-9]+).*"; .vers)'
"0.2.1"

As the documentation says, the arguments to regex filters must be valid jq strings, which basically means valid JSON strings but allowing for interpolation. In other words, you need to escape the . with two backslashes.

All 2 comments

$ echo "{\"VERSION\": \"0.2.1-alpha+abxc23\"}" |\
  jq '.VERSION | sub("(?<vers>[0-9]+\\.[0-9]+\\.[0-9]+).*"; .vers)'
"0.2.1"

As the documentation says, the arguments to regex filters must be valid jq strings, which basically means valid JSON strings but allowing for interpolation. In other words, you need to escape the . with two backslashes.

Ah ok. I tried that, but I thought I had to escape the backslash as well, trying only with ".", and not just ".". Thanks for clarifying.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benjamin-bin-shen picture benjamin-bin-shen  路  3Comments

rubensayshi picture rubensayshi  路  3Comments

thedward picture thedward  路  3Comments

kaihendry picture kaihendry  路  4Comments

neowulf picture neowulf  路  3Comments