CodeIgniter Forums
Regex Help! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Regex Help! (/showthread.php?tid=52785)



Regex Help! - El Forum - 06-26-2012

[eluser]beaudierman[/eluser]
I'm trying to set up a custom 404 page using the 404_override option. Trouble is, I have a route set up as (:any) which apparently completely breaks 404_override. If I remove the (:any) rule, it works fine. So, I need to eliminate my (:any) call and instead be specific about what I want for that rule, the problem is that regex doesn't seem to work very well.

Basically I just need to match 3 digits, followed by a dash, then anything.

What I have:
Code:
$route['^\d{3}-(.*)$'] = 'product/index/$1';

I've also tried:
Code:
$route['(:num)-(:any)'] = 'product/index/$1';

Which works great on regexpal, but, not on CI. My regex is very rusty so if anyone has any suggestions I'd greatly appreciate it.


Regex Help! - El Forum - 06-26-2012

[eluser]Matalina[/eluser]
did you try to take out the start ^ and end $ characters?


Regex Help! - El Forum - 06-26-2012

[eluser]beaudierman[/eluser]
Yes I have.


Regex Help! - El Forum - 06-26-2012

[eluser]Matalina[/eluser]
here's another method...

[0-9]{3}-(.*)

if that doesn't work escape the -


Regex Help! - El Forum - 06-26-2012

[eluser]beaudierman[/eluser]
[quote author="Matalina" date="1340744105"]here's another method...

[0-9]{3}-(.*)

if that doesn't work escape the -[/quote]

Aha! It worked but was then only bringing in the
Code:
(.*)
half of my query string. I changed the $1 to $0 and all works! Thank you!


Regex Help! - El Forum - 06-26-2012

[eluser]Matalina[/eluser]
it is only bringing in the second half because of the parenthesis.

([0-9]{3}-.*) should allow $1 to be used but it would be the same as $0 anyway.

http://myregextester.com/ will show you what information falls into what part of the array.

and unless you plan on having a uri that looks like

123-

I would use [0-9]{3}-.+


Regex Help! - El Forum - 06-26-2012

[eluser]InsiteFX[/eluser]
Here is another Regex tester

Look at the bottom right and you can download a Windows desktop version of it.