6

I've made this regex:

^[a-zA-Z0-9_.-]*$

Supports:

letters [uppercase and lowercase]
numbers [from 0 to 9]
underscores [_]
dots [.]
hyphens [-]

Now, I want to add these:

spaces [ ]
comma [,]
exclamation mark  [!]
parenthesis [()]
plus [+]
equal [=]
apostrophe [']
double quotation mark ["]
at [@]
dollar [$]
percent [%]
asterisk [*]

For example, this code accept only some of the symbols above:

^[a-zA-Z0-9 _.,-!()+=“”„@"$#%*]*$

Returns:

Warning: preg_match(): Compilation failed: range out of order in character class at offset 16

youmotherhaveapples
  • 347
  • 2
  • 3
  • 10

5 Answers5

22

Make sure to put hyphen - either at start or at end in character class otherwise it needs to be escaped. Try this regex:

^[a-zA-Z0-9 _.,!()+=`,"@$#%*-]*$

Also note that because * it will even match an empty string. If you don't want to match empty strings then use +:

^[a-zA-Z0-9 _.,!()+=`,"@$#%*-]+$

Or better:

^[\w .,!()+=`,"@$#%*-]+$

TEST:

$text = "_.,!()+=,@$#%*-";
if(!preg_match('/\A[\w .,!()+=`,"@$#%*-]+\z/', $text)) {
   echo "error.";
}
else {
   echo "OK.";
}

Prints:

OK.
anubhava
  • 761,203
  • 64
  • 569
  • 643
4

The hyphen is being treated as a range marker -- when it sees ,-! it thinks you're asking for a range all characters in the charset that fall between , and ! (ie the same way that A-Z works. This isn't what you want.

Either make sure the hyphen is the last character in the character class, as it was before, or escape it with a backslash.

I would also point out that the quote characters you're using “”„ are part of an extended charset, and are not the same as the basic ASCII quotes "'. You may want to include both sets in your pattern. If you do need to include the non-ASCII characters in the pattern, you should also add the u modifier after the end of your pattern so it correctly picks up unicode characters.

Spudley
  • 166,037
  • 39
  • 233
  • 307
3

Try escaping your regex: [a-zA-Z0-9\-\(\)\*]

Check if this help you: How to escape regular expression special characters using javascript?

Community
  • 1
  • 1
Jordi
  • 189
  • 6
  • None of the newly-added characters need to be escaped when they appear inside a character class (which they do). The only problematic character is the hyphen, and the other responders have explained how to deal with that. – Alan Moore Sep 06 '13 at 14:51
  • Oh, and possibly the fancy quotes (`“”„`). But [Spudley's answer](http://stackoverflow.com/a/18660140/20938) deals with those. – Alan Moore Sep 06 '13 at 15:00
2

Inside of a character class [...] the hyphen - has a special meaning unless it is the first or last character, so you need to escape it:

^[a-zA-Z0-9 _.,\-!()+=“”„@"$#%*]*$

None of the other characters need to be escaped in the character class (except ]). You will also need to escape the quote indicating the string. e.g.

'/[\']/'
"/[\"]/"
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

try this

^[A-Z0-9][A-Z0-9*&!_^%$#!~@,=+,./\|}{)(~`?][;:\'""-]{0,8}$

use this link to test

trick is i reverse ordered the parenthesis and other braces that took care of some problems. And for square braces you must escape them

Community
  • 1
  • 1
WPFKK
  • 1,419
  • 3
  • 20
  • 42