Solved

MySQL REGEXP

Posted on 2014-01-11
3
433 Views
Last Modified: 2014-01-11
I'm trying to match against urls like

/my-url
/my-url-1


What I have so far (I hate regex)
select id,CAST(REPLACE(url, 'my-url-', '') AS UNSIGNED) as urls from tbl_url
where url REGEXP '^/my-url-[0-9]+$'


I need to make the last dash followed by numbers optional, so if there is just this in the db
/my-url
Then I end up with 0 as urls
If this exists in the db
/my-url-1
Then I would end up with 1 as urls
0
Comment
Question by:Gary
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39773699
Try this:
select id,CAST(REPLACE(REPLACE(url, '/my-url-', ''), '/my-url', '0') AS UNSIGNED) as urls from tbl_url
where url REGEXP '^/my-url(-[0-9]+)?$'

Open in new window

0
 
LVL 58

Author Comment

by:Gary
ID: 39773708
Seems to work fine, will just do a bit more testing
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 39773728
Yep works fine

Thanks

p.s. the casting works fine as is, doesn't need the additional replace.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now