tags:

views:

125

answers:

6
$rex = '/^[^<,"@?=>|;#]$/i';

I'm having trouble with this regular expression. The idea is that the input fields are checked for certain characters and if present, throw an error.

This regex is throwing errors for every string longer than 1 character. Can anyone tell me what I'm doing wrong?

EDIT: People are saying they don't see what I want to do with this regex. What I want to do is refuse the input if one of the following characters is part of the entered string:

< > , " @ ? = | ; #

EDIT2: JG's "valid" regex does the trick.

+5  A: 

You have the $ after your expression and the ^ in the beginning, which means you are accepting exactly one character.

EDIT (based on the comments):

You can try to see if your input fields only have valid characters, by matching it against this (if it matches, it means there are no invalid characters):

$rex = '/^[^<,"@$?=>|;#]+$/i'

You can also do the reverse, that is, test if your input fields have any invalid characters, using the regex provided by chaos:

$rex = '/[<,"@$?=>|;#]/';

This way, if the regex matches, then it means you have invalid characters.

JG
So removing the $ should do the trick?
WebDevHobo
You probably want to remove both the ^ and the $. If you remove the $ while keeping the ^, you will only match one of the characters at the beginning of the expression.
Michael Mior
Removing both the ^ and $ means it'll validate any string, as long as it contains a valid character, and is thus not what is needed.
Sebastian P.
You can try to see if your input fields only have **valid** characters, by matching it against this: `'/^[^<,"@$?=>|;#]+$/i'`. You can also do the reverse, that is, test if your input fields have any **invalid** characters, using the regex provided by chaos: `$rex = '/[<,"@$?=>|;#]/';`. This way, if the regex matches, then it means you have invalid characters.
JG
The invalid regex shows no matches, no matter if I enter a correct or incorrect input. My username for instance, is a false preg_match()
WebDevHobo
The valid regex seems to do the trick.
WebDevHobo
+1  A: 

You'll want '/^[^<,"@$?=>|;#]+$/i' or '/^[^<,"@$?=>|;#]*$/i'.

For an example:

$valid = 'Hello';
$invalid = 'h<i';
$regex = '/^[^<,"@$?=>|;#]+$/i';

print "'$valid' gives ".preg_match($regex, $valid)."\n";
print "'$invalid' gives ".preg_match($regex, $invalid)."\n";

Which outputs:

'Hello' gives 1
'h<i' gives 0

What I simply did was take your expression and add a + or * after your character group.

In regular expressions, * means match 0 or more occurences, and + means match 1 or more.

Since ^ means the beginning of the string and $ the end, without a + or *, you tell it to match a string the consists of exactly one occurrence of a non-special character, thus why it errors if your string is longer than one character.

If you wish, you can also remove the i at the end of the expression, as you don't need to do a case-insensitive match when no letters are involved in your expression.

For more information on regular expressions, take a look at Regular-Expressions.info.

Sebastian P.
Now nothing matches. No character on my keyboard provides a match.
WebDevHobo
That seems quite strange to me. I will edit in a sample that works for me.
Sebastian P.
A: 

^ means beginning of line
$ means end of line
[] means match one out of a group of character

You will match lines containing one and only one among that list.

Stefano Borini
So I'll have to escape the dollar sign in the middle?
WebDevHobo
No, so you have to qualify the set to ask for multiples. Put either + or * after the exclusion set to say you want one-or-more or zero-or-more, respectively.
hughdbrown
+2  A: 

What you probably actually need is:

$rex = '/[<,"@$?=>|;#]/';

Then your error case is when this regex matches, not when it doesn't.

This is equivalent to doing what you're currently doing with this small change to your regex:

$rex = '/^[^<,"@?=>|;#]*$/i';

This is kind of nonsensically overcomplex, though. Like trying to find out whether there's an elephant in the room by counting how many things in the room aren't elephants, then seeing if that number is the same as the number of things in the room. (And the /i modifier is not, at any time, accomplishing anything.)

chaos
A: 

maybe "/(.*?)[^<,"@$?=>|;#]/i" or "/^[^<,"@$?=>|;#].*+$/i"

don't know exactly what you are trying to do

Tom
All those random character are characters I wish to forbid. If any single 1 of them appears, the input is refused. If non present, the input is accepted.
WebDevHobo
+1  A: 

You're asking for only one character. If you want multiple characters, get the pattern repeated, either like this (one or more times):

 $rex = '/^[^<,"@?=>|;#]+$/i';

or like this (zero or more times):

 $rex = '/^[^<,"@?=>|;#]*$/i';
hughdbrown