Alexander_F
Alexander_F

Reputation: 2877

Mysql regex looking for links

How to get this expression work:

SELECT post_content 
FROM wp_posts 
WHERE post_content REGEXP 'http:\/\/www.google.com\/\?link=.*_1'

I have some posts with some links in this style, so I wont get list of all posts with this links.

With this query I get an empty list.

Table:

| id | wp_posts | other fields....
| 1  | text <a href="http://www.google.de/?link=test_1">Link</a> text | ....

So I have to find the post ID 1

Upvotes: 0

Views: 769

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180987

Escaping / is not necessary, but you'll need to use \\ to escape your question mark.

Note
Because MySQL uses the C escape syntax in strings (for example, “\n” to represent the newline character), you must double any “\” that you use in your REGEXP strings.

Also, searching for google in the correct country is a good idea :-)

This'll work;

SELECT post_content 
FROM wp_posts 
WHERE post_content REGEXP 'http://www.google.de/\\?link=.*_1';

An SQLfiddle to test with.

Upvotes: 1

Related Questions