![](/img/trans.png)
[英]How can i extract Dates from a string field in Hive using RegEx function
[英]Extract string from text field in PostgreSQL (using regex?)
我有很多有city
和direction
字段的行。 但是从旧的进口来看,城市和方向在direction
领域是混合的。 喜欢:
dir number, extra data, CITY, AL 111111 dir number, CITY, AL 111111 number, dir, number, CITY, dir number, CITY, AL 111111
重要的是'CITY'总是出现在美国邮政编码之前,我想提取它并将其保存在city
领域并使用UPDATE
(使用正则表达式?)。 可能吗?
就像是:
update TABLE set city = SOME_REGEX_MAGIC_FROM_DIRECTION_FIELD
where direccion ~ 'REGEX_MAGIC'
工作的SQL语句:
update TABLE
set city = substring(direction FROM '([^,]+),[^,]+$')
where direction like '%, __ _____';
如果你想要在最后一个逗号之前的部分,一种方式(很多)是使用普通的substring()
调用(regexp变体):
substring(direction FROM ',([^,]+),[^,]+$') AS city
您的UPDATE
语句可能如下所示:
UPDATE tbl
SET city = substring(direction FROM ',([^,]+),[^,]+$')
WHERE direction ~ ', *\D\D \d{5}$'
从你的数据我会得到你需要这样的东西:
SELECT regexp_matches('direction_field', '([^,]+) \d{5}');
从Redshift中的正则表达式获取子字符串:
SELECT REGEXP_SUBSTR(
'hello_uuid_092bab12-8d8b-40ad-b8b7-bc9f05e52c9c_something_else',
'([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})'
)
结果: 092bab12-8d8b-40ad-b8b7-bc9f05e52c9c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.