I have problem with allowing specified characters in preg_match. I've tried making following pattern: /^[A-Za-z0-9 !@#$%&()-_\\[\\]:;\\"'|,.\\?\\/]/
Right now it allows everything, even * which is not there.
I know that there's this rule that before regex specified characters I have to put "\\"
before character. Correct me if I'm wrong.
Can someone explain me how does this work?
I want to allow this chars: AZ az 0-9 !@#$%&()-_[]:;"'|,.?/ (and of course spaces)
And exclude this: ~`^*+={}<>\\
An unescaped hyphen needs to be at first or last position in a character class or it needs to be escaped. Otherwise it is considered a range. So use:
/^[A-Za-z0-9 !@#$%&()_\[\]:;\"'|,.\?\/-]/
In your regex /^[A-Za-z0-9 !@#$%&()-_\\[\\]:;\\"'|,.\\?\\/]/
where -
is in the middle of and )
(ASCII: 41) and _
(ASCII: 95), hence matching all the characters in this range.
Also you need to use anchors to match entire input:
/^[A-Za-z0-9 !@#$%&()_\[\]:;\"'|,.\?\/-]+$/
This part )-_
in the character class is a range of characters.
From )
to _
.
You probably should escape the dash .. )\\-_
then its just a character.
41 29 ) 73 49 I
42 2A * 74 4A J
43 2B + 75 4B K
44 2C , 76 4C L
45 2D - 77 4D M
46 2E . 78 4E N
47 2F / 79 4F O
48 30 0 80 50 P
49 31 1 81 51 Q
50 32 2 82 52 R
51 33 3 83 53 S
52 34 4 84 54 T
53 35 5 85 55 U
54 36 6 86 56 V
55 37 7 87 57 W
56 38 8 88 58 X
57 39 9 89 59 Y
58 3A : 90 5A Z
59 3B ; 91 5B [
60 3C < 92 5C \
61 3D = 93 5D ]
62 3E > 94 5E ^
63 3F ? 95 5F _
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.