KdgDev
KdgDev

Reputation: 14529

Regex allows only 1 character

$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.

Upvotes: 2

Views: 3702

Answers (6)

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.

Upvotes: 1

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91299

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.

Upvotes: 5

chaos
chaos

Reputation: 124297

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.)

Upvotes: 2

hughdbrown
hughdbrown

Reputation: 49013

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';

Upvotes: 1

Tom
Tom

Reputation: 7091

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

don't know exactly what you are trying to do

Upvotes: 0

Stefano Borini
Stefano Borini

Reputation: 143795

^ 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.

Upvotes: 0

Related Questions