Page 1 of 1
I hate regex please help!
Posted: Sat Aug 16, 2008 11:54 am
by andylyon87
Hi guys and gals,
I'm getting so fed up, all I want is a simple expression to allow A-Z a-z 0-9 .-!?@, and it should be between 4 and 60 characters long how come this doesn't work:
Code: Select all
if (eregi ('^[a-zA-Z0-9,.-!?@]{4,60}$', $blah)
please help I have been reading tutorials for about an hours now and I cant get anything to work!!
Re: I hate regex please help!
Posted: Sat Aug 16, 2008 12:44 pm
by prometheuzz
When posting in a public forum, it helps to say HOW it doesn't work. So you could give the example string that doesn't, but should match.
I see one error: inside a character class, the hyphen has a special meaning (a range indicator) except at the start or end of a character class:
Code: Select all
[a-c] // matches 'a', 'b' or 'c'
[-ac] // matches '-', 'a', 'c'
[ac-] // the same as the previous
Re: I hate regex please help!
Posted: Sat Aug 16, 2008 1:34 pm
by andylyon87
Sorry dude, I am trying to pass a word into it and that is literally it as explained in the top post.
It literally just doesn't work, keeps coming up with the else statement!
Code: Select all
if (eregi ('^[a-zA-Z0-9,-.@?!_]{4,60}$', stripslashes(trim($_POST['new_cat'])))) {
$c = str_replace(" ","_",escape_data($_POST['new_cat']));
} else {
$c = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid category!</font></p>';
}
above would be where new_cat is simply a word or phrase for a new catgory, it doesn't pose me a useful error just the else statement.
Re: I hate regex please help!
Posted: Sat Aug 16, 2008 1:51 pm
by prometheuzz
andylyon87 wrote:Sorry dude, I am trying to pass a word into it and that is literally it as explained in the top post.
...
... it doesn't pose me a useful error just the else statement.
I get the impression you didn't read my relpy at all! Because I didn't ask for an error message, I asked to supply a string that doesn't match but should match according you. And also, I told you at least one mistake in your regex, yet I still see it in your last post.
Code: Select all
<?php
if(eregi('^[a-zA-Z0-9,.@?!_-]{4,60}$', 'aB-?!')) {
echo "It does work.";
}
?>
The string "It does work." is printed, so as you see, it
does work!
; )
Re: I hate regex please help!
Posted: Sun Aug 17, 2008 3:03 am
by prometheuzz
You're welcome...