Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL REGEXP

Posted on 2014-01-11
3
Medium Priority
?
477 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

609 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