tags:

views:

18

answers:

2

So what i want to do is use a Regex to select only certain parts of data from a column

Example :

SELECT URL REGEXP 'http://' from websitelist --(website list is a list of URL's)

If I run that against the table it returns 1 foreach row in which 'htt://' was found, what I want is to return the string that matches the regexp

+1  A: 

The REGEXP operator performs a match and returns 0 or 1 based on whether the string matched the expression or not. It does not provide a way to extract the matched portion. I don't think that there is any way to extract the matched portion.

D.Shawley
+1  A: 

You could use just use string functions if it's as simple as your example - just removing http://

SELECT REPLACE(URL, 'http://', '') AS url FROM websitelist;

Probably faster as there is overhead for the REGEX engine.

Jason McCreary
I dont want to replace text , rather just return text that matches the regexp I pass in
RC1140
Just to be clear, this only replaces it for your `SELECT`. It doesn't actually update the field. So you want to return http://?
Jason McCreary
I got the part about it not updating the data, Yes i only want to return what text would match my regexp , in this case the 'http://' , but it could be anything
RC1140
As **D.Shawley** said, the `REGEXP` functions only return true or false. Not the match. You would need to build this into the query yourself: `IF(URL REGEXP 'http://', 'http://', '')` or just alias the field and interpret it in your results: `URL REGEXP 'http://' AS has_http`. If you have many matches, I suggest doing this with a different technology.
Jason McCreary
It seems like I am going to have to do it code side , was hoping to minimize processing but its fine
RC1140