Forum Moderators: phranque

Message Too Old, No Replies

Regular Expression Help

         

skinter

9:29 pm on May 18, 2006 (gmt 0)

10+ Year Member



I'm trying to learn regex but I can't seem to solve this simple problem.

I want to validate a query string using a whitelist, so if a character is not on the 'ok' list, the string is rejected. The problem is, I can't get my expression to work *properly*.

I want to have a string using alphanumeric characters and possible followed by a single slash and a number. (the slash can't be the first or last character)

so alpha_bet
or alpha_bet/10

The rejected ones could be:

alpha_bet/10/
alpha_bet/%2e%2e%2f
etc.

Here's what I have so far:
if( ereg( "[^a-zA-Z0-9_]+?([^\/([0-9]+))", $id ) )

This rejects the valid alpha_bet/10, but allows two slashes. I have no idea what to do because it looks right to me, and Google has only brought up tutorials that seem to validate my false assumption.

All help would be greatly appreciated.

skinter

10:05 pm on May 18, 2006 (gmt 0)

10+ Year Member



Ah hah! I got it!

It looks a little inneficient, but it works. If you are having the same trouble I was, here's what I did:

if(!ereg( "^[a-zA-Z0-9_]+$¦^[a-zA-Z0-9_]+/[0-9]+$", $id ) )