php regular expression for "|" and digits
if (!preg_match('/\|d/', $ids)) {
$this->_redirect(ROOT_PATH . '/commission/payment/active');
}
T开发者_JS百科hank you in advance. Sorry for my english.
Looking at your example I assume you are looking for a regex to match string that begin and end with numbers and numbers are separated with |
. If so you can use:
^\d+(?:\|\d+)*$
Explanation:
^ - Start anchor.
\d+ - One ore more digits, that is a number.
(? ) - Used for grouping.
\| - | is a regex meta char used for alternation,
to match a literal pipe, escape it.
* - Quantifier for zero or more.
$ - End anchor.
The regex is:
^\d[|\d]*$
^ - Start matching only from the beginning of the string
\d - Match a digit
[] - Define a class of possible matches. Match any of the following cases:
| - (inside a character class) Match the '|' character
\d - Match a digit
$ - End matching only from the beginning of the string
Note: Escaping the |
is not necessary in this situation.
A string that contains only |
or digits and begins with a digit is written as ^\d(\||\d)*$
. That means: either \|
(notice the escape!) or a digit, written as \d
, multiple times.
The ^
and $
mean: from start to end, i.e. there’s no other character before or after that.
I think /^\d[\d\|]*$/
would work, however, if you always have three digits separated by bars, you need /^\d{3}(?:\|\d{3})*$/
.
EDIT:
Finally, if you always have sequences of one or more number separated by bars, this will do: /^\d+(?:\|\d+)*$/
.
精彩评论