a-Z0-9()[]^-_.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

a-Z0-9()[]^-_.

Post by JKM »

Could anyone please give me the regex 'code' for allowing only

Code: Select all

a-Z 0-9 - _ ( ) [ ] { } . ^

Code: Select all

if(!preg_match("A-Za-z0-9_^(){}[]-|~.", $_GET['bn'])) {
    //error
}
 
/* index.php?bn=Hey returns:
 
    Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash
 
*/
freamer
Forum Newbie
Posts: 5
Joined: Mon Oct 05, 2009 3:08 am

Re: a-Z0-9()[]^-_.

Post by freamer »

Why not using something like? /^[a-z0-9()\[\]\^\-\_\.]+$/s

Read docs, the task you`re asking about is very easy.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: a-Z0-9()[]^-_.

Post by ridgerunner »

This one should do the trick...

Code: Select all

if (!preg_match('/^[A-Za-z0-9\-_()[\]{}.^]+$/', $_GET['bn'])) {
    //error
}
freamer's code ('/^[a-z0-9()\[\]\^\-\_\.]+$/s') almost works (but fails to match uppercase letters or the curly braces).
Post Reply