?
Solved

MySQL REGEXP

Posted on 2014-01-11
3
Medium Priority
?
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

801 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