我有一个需要过滤的用户代理列表。但是我没有做到。
例如,下面有5个useragent。表的名称为temp,列名称为useragent。我想在postgresql中返回前3个。
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1"
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) GSA/33.0.164895372 Mobile/14G60 Safari/602.1"
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1"
"POF 6.21.912; (iPhone8,2; iOS 10.3.3; en_US; g=ON; p=ON; r=WiFi) 0ADC1470-4814-4100-83A6-D9D4843F2DFA;"
"POF 6.21.912; (iPhone9,2; iOS 10.3.3; en_US; g=ON; p=ON; r=WiFi) D7CBD4C8-3482-4226-93DB-B8894AFFD087;"
这是我正在尝试的代码:
select useragent from temp where useragent like `'^Mozilla/\\d\\.\\d\\s\\(iPhone*'`
它什么也不返回。谁能帮我解决这行语法吗?
答案 0 :(得分:0)
请勿使用like
,like
只能检查一个字符串(例如'%abc%')是否包含在另一个字符串(例如'kkabchh')中,并且不适用于正则表达式。您应该在~
中使用PostgreSQL
,如下所示:
postgres=# select 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1' ~ 'Mozilla.*iPhone.*' as bool;
bool
------
t
(1 row)
postgres=# select useragent from temp where useragent ~ 'Mozilla.*iPhone.*';
useragent
------------------------------------------------------------------------------------------------------------------------------------------------
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) GSA/33.0.164895372 Mobile/14G60 Safari/602.1
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1
(3 rows)