我正在使用具有以下结构的URL字符串:
Page State City
page wa seattle
page ca sandiego
page mi detroit
我基本上想知道是否可以将Regex与case语句结合使用来创建以下内容:
CASE WHEN (regexp_instr(HITSPAGEPAGEPATH::text, '^/page/[a-z]{2}/[a-z]+'::
CHARACTER VARYING::text))
THEN (regexp_instr(HITSPAGEPAGEPATH::text,
'^/page/[a-z]{2}/[a-z]+'::CHARACTER VARYING::text))
ELSE NULL
END AS city
我目前编写的代码可以删除哪些页面是状态页面,哪些页面是城市页面。
$scope.init = function () {
console.log("init");
$http.get('json/file.json') .success(function(data) {
$scope.jsonDat = res.data;
})
.error(function(data,status,error,config){
$scope.jsonDat = [{heading:"Error",description:"Could not load json data"}];
});
console.log(jsonDat);
};
我无法弄清楚的部分是我可以放在“then”之后只显示城市或州。这是使用sql workbench在Amazon redshift上的postgres sql,如果这有助于回答什么语法in。
答案 0 :(得分:1)
不需要正则表达式,只需将字符串拆分为3个元素(由/
分隔)并将每个元素用作列:
select elements[1] as page,
elements[2] as state,
elements[3] as city
from (
select string_to_array(hitspagepagepath, '/') as elements
from the_table
) t
order by page;
答案 1 :(得分:0)
select hitspagepagepath, split_part(hitspagepagepath,'/',2) as root_url,
split_part(hitspagepagepath,'/',3) as State,
split_part(hitspagepagepath,'/',4) as City
from Table