Solved

MySQL REGEXP

Posted on 2014-01-11
3
440 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now