简体   繁体   English

根据另一列查询到mysql中的REGEX_REPLACE

[英]Query to REGEX_REPLACE in mysql as per another column

I have a table T with two columns C1 and C2. 我有一个带有两列C1和C2的表T。

I want to write a query as follows: 我想编写一个查询,如下所示:

UPDATE TABLE T 
SET C2 = REGEX_REPLACE(
    "(REG_SUB_PART1)(REG_SUB_PART2)(REG_SUB_PART3)", 
    C1, 
    REG_SUB_PART1
) 
WHERE C2="ABC";

Effectively, I want to use another column C1, let's say URL " http://www.google.com " and set C2 to be a part of it, let's say "google.com" using $3 (third part) of regex "(http://)?(www\\.)?([a-zA-Z0-9]*)" . 实际上,我想使用另一列C1,假设URL为“ http://www.google.com ”,并将C2设置为其中的一部分,例如,使用$3 (第三部分)的regex, $3 “ google.com "(http://)?(www\\.)?([a-zA-Z0-9]*)"

As a result, C2 should be set as "google.com". 因此,C2应该设置为“ google.com”。

How could it be done using MySql? 如何使用MySql完成?

PS: Please don't concentrate on specific regex. PS:请不要专注于特定的正则表达式。

There is no way to do regex capture groups in MySQL per http://dev.mysql.com/doc/refman/5.0/en/regexp.html . 无法通过http://dev.mysql.com/doc/refman/5.0/en/regexp.html在MySQL中进行正则表达式捕获组。 However, if you know the starting index in c1, an alternate way to accomplish a similar thing would be. 但是,如果您知道c1中的起始索引,则可以使用另一种方法来完成类似的操作。

update t set c2= substring(c1,12) where c2 = 'abc'; 更新t set c2 = substring(c1,12)其中c2 ='abc';

This will get the substring 'google.com' from ' http://www.google.com ', provided that was the value in column c1. 如果这是c1列中的值,它将从“ http://www.google.com ”获取子字符串“ google.com”。

Some other MySQL string functions that might come in handy are substring, substring_index and locate. 可能会派上用场的其他一些MySQL字符串函数是substring,substring_index和locate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM