I’m making a news system that uses unique slugs to identify the article. When creating a new article, I need to make sure the unique slug is not already in use. So if two articles have the exact same name, they will therefore generate the exact same slug. I want to append a number to the end of the slug in the event it is in use.
Like so:
- some-really-interesting-article
- some-really-interesting-article-1
- some-really-interesting-article-2
And so on. So in my test case, I’m selecting all records from the database that exactly match “some-really-interesting-article” with 0 or more “-number”. So in this case I will return 3 rows, so the next slug would be “some-really-interesting-article-3”.
This works great except my regex is behaving peculiar (or likely very normal, and I just suck at regex) and is also return rows with partial matches. So if I search “some-really-interesting”, that will pass.
SELECT id, title, slug
FROM news
WHERE slug RLIKE '([[:<:]]some-really-interesting-article[[:>:]][-\d]*)'
So as I said, if I run the above regex with “some-really-interesting” it will return all 3 rows.
Please tell me that I am blatantly and ignorantly doing wrong. Thanks.
What about this ?
Hopefully this will work 🙂