问题描述
运行模式匹配验证时出现以下错误:
I get the following error running a pattern matching validation:
ErrorException [ Warning ]: preg_match() [function.preg-match]: No ending matching delimiter ')' found
我使用的PREG正则表达式已被使用从在线正则表达式库中获取,但是我找不到丢失的定界符在哪里。有人能指出吗?
The PREG regex that I use has been taken from on online regex library but I can't find where the missing delimiter is. Is anyone able to point it out please?
检查英国邮政编码:
(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)
如果我使用这种添加验证规则的方法,则会得到错误未知修饰符'$':
$ val-> add('post_code ','邮政编码')
-> add_rule('required')
-> add_rule('match_pattern','(((^ [BEGLMNS] [1-9] \d? )|(^ W [2-9])|(^(A [BL] | B [ABDHLNRST] | C [ABFHMORTVW] | D [ADEGHLNTY] | E [HNX] | F [KY] | G [LUY] | H [ADGPRSUX] | I [GMPV] | JE | K [ATWY] | L [ADELNSU] | M [EKL] | N [EGNPRW] | O [LX] | P [AEHLOR] | R [GHM] | S [AEGKL -PRSTWY] | T [ADFNQRSW] | UB | W [ADFNRSV] | YO | ZE)\d\d?)|(^ W1 [A-HJKSTUW0-9])|(((^ WC [ 1-2])|(^ EC [1-4])|(^ SW1))[ABEHMNPRVWXY]))(\s *)?([0-9] [ABD-HJLNP-UW-Z] {2 }))$ |(^ GIR\s?0AA $)\)');
If I use this method of adding my validation rule, I get the error unknown modifier '$':$val->add('post_code', 'post code') ->add_rule('required') ->add_rule('match_pattern', '(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)\)');
我使用它的原始方式是简写版本:
$ val-> add_field('post_code','post code','required | match_pattern [((((^^ [BEGLMNS] [1-9] \ d?)|(^ W [2-9])|(^(A [BL] | B [ABDHLNRST] | C [ABFHMORTVW] | D [ADEGHLNTY] | E [HNX] | F [KY] | G [LUY ] | H [ADGPRSUX] | I [GMPV] | JE | K [ATWY] | L [ADELNSU] | M [EKL] | N [EGNPRW] | O [LX] | P [AEHLOR] | R [GHM] | S [AEGKL-PRSTWY] | T [ADFNQRSW] | UB | W [ADFNRSV] | YO | ZE)\d\d?)|(^ W1 [A-HJKSTUW0-9])|(((^ WC [1 -2])|(^ EC [1-4])|(^ SW1))[ABEHMNPRVWXY]))(\s *)?([0-9] [ABD-HJLNP-UW-Z] {2} ))$ |(^ GIR\s?0AA $)\)]');
The original way I was using it was the shorthand version:$val->add_field('post_code', 'post code', 'required|match_pattern[(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)\)]');
$ val
是模型的验证实例。
$val
is a validation instance of a Model. I'm using FuelPHP if that's of any use.
推荐答案
<?php
$regex = '/(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)/';
var_dump( preg_match( $regex, 'M1 1AA' ), preg_match( $regex, 'not valid' ) );
对我来说就像一个魅力。您需要有定界符。
Works like a charm for me. You need to have delimiters in place.
这篇关于未找到PregMatch没有结尾的')'分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!