首页 > 解决方案 > InfluxDB如何转义方括号“[”

问题描述

我正在查询流入数据库,如下所示,

select * from measurement where '/cda/stats/@name' =~ /cda\/stats.*/ limit 5;

上面的查询工作正常,但是当我尝试在查询字符串中指定方括号时它不起作用,例如"/cda/stats/[name='set']

select * from mgmgrand where '/cda/stats/@name' =~ /cda\/stats[name='set'].*/ limit 5;

不确定如何在上述查询中转义方括号。

反斜杠“\”不适用于方括号。

标签: influxdbinfluxql

解决方案


我在 influx DB 中对此进行了测试,发现了一个奇怪的解决方案(我不知道它为什么会起作用)。如果您的标识符未加引号,将单引号更改为双引号,则替换'/cda/stats/@name'"/cda/stats/@name"似乎可以解决此问题。

select * from mgmgrand where "/cda/stats/@name" =~ /\[/
//matches the value /cda/stats/[name='set']

在不更改引号的情况下,它适用于没有 [ 的值,但在尝试匹配 [ 字符时不起作用。

select * from mgmgrand where '/cda/stats/@name' =~ /cda/
//matches the value /cda/stats/[name='set'] 
select * from val1 where '/cda/stats/@name' =~ /\[/
//does not give an output

如果您使用带引号的标识符替换'/cda/stats/@name'"'/cda/stats/@name'"修复"\"/cda/stats/@name\""它。


推荐阅读