是字符集正则表达式

I am working on a project where i have to validate the BECS characters. Bulk Electronic Clearing System (BECS) only allowed the following characters.

BECS Character set

Type                        Description
Numeric                     0 to 9
Alphabetic                  Uppercase A to Z and Lowercase a to z
+                           Plus sign
-                           Minus sign or hyphen
@                           At sign
SP                          Blank space
$                           Dollar sign
!                           Exclamation mark
%                           Percentage sign
&                           Ampersand
(                           Left Parenthesis
)                           Right Parenthesis
*                           Asterik
.                           Period or decimal point
/                           Solidus (slash)
#                           Number Sign (Pound or Hash)
=                           Equal Sign
:                           Colon
;                           Semicolon
?                           Question mark
,                           Comma
’                           Apostrophe
[                           Left square bracket
]                           Right square bracket
_                           Low line (underscore)
^                           Circumflex

I have tried the following but not working:

preg_match("/^[A-Za-z0-9^_[]',?;:=#/.*()&%!$ @+-]+$/", $string);

Inside character classes, you don't need to escape most of the metacharacters.

/^[A-Za-z0-9^_[\]',?;:=#\/.*()&%!$ @+-]+$/
  • ] is escaped to prevent it from closing the character class
  • / needs to be escaped because we're using it as regex delimiter
  • - doesn't need to be escaped because it's the last character in the class
  • ^ doesn't need to be escaped because it's not the first character in the class


Or, if you want a shorter expression, the following regex covers the same range:

/^[ !#-;=?-[\]^_a-z]+$/

you have to escape most of the chars in your string as they have special meaning in regular expressions

(of course you were right not to escape the leading ^ and the trailing $ as they indicate that no other character can be in the line):

preg_match("/^[A-Za-z0-9\\^_\\[\\]',\\?;:=#/\\.\\*\\(\\)\\&\\%!\\$ @\\+\\-]+$/", $string);

For the record a list of the BECS allowed chars vs regular expression chars:

Type                        Description
Numeric                     0 to 9
Alphabetic                  Uppercase A to Z and Lowercase a to z
+                           Plus sign: means 1 or more
-                           Minus sign or hyphen: used for char range
$                           Dollar sign: end of line
(                           Left Parenthesis: starts a group
)                           Right Parenthesis: ends a group
*                           Asterik: 0 or more
.                           Period or decimal point: any char (not in a range, would work)
?                           Question mark: 0 or 1
[                           Left square bracket: starts a range
]                           Right square bracket: ends a range
^                           Circumflex: not/line start

Instead of worrying about escaping manually use preg_quote

The code would then be preg_match("/^[A-Za-z0-9".preg_quote("^_[]',?;:=#/.*()&%!$ @+-", "/")."]+$/", $string);