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
  • There is no way to do this in mysql Commented Jul 4, 2010 at 18:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

5 Comments

I dont want to replace text , rather just return text that matches the regexp I pass in
Just to be clear, this only replaces it for your SELECT. It doesn't actually update the field. So you want to return http://?
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
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.
It seems like I am going to have to do it code side , was hoping to minimize processing but its fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.