Solved

MySQL REGEXP

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

740 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