PHP Regular Expression Issue

Hi,
My following regular expression is causing an Unknown modifier '|' error.

preg_match '(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)', $this);

Open in new window


It's suppose to validate either US or Canadian postal codes.

Anyone know why the | conditional is not functioning (or any other code error)?

Thanks!
christamccAsked:
Who is Participating?
 
Marco GasiFreelancerCommented:
Try to modify it ths way:

preg_match '#(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)#', $this);
                         
Cheers
0
 
christamccAuthor Commented:
Thanks marqusG, it worked!  I'm going to Accept your solution, but can you let me know one thing?  Is the "#" pretty much the required deliminator when working with the 'or' conditional?
0
 
Marco GasiFreelancerCommented:
Not exactly: you have to use something sign is not used in regex and in your regex you didn't use anything. You would had just used '/'

preg_match '/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/', $this);

or something else. The most used delimiter are '#' and '/'.

Cheers
0
 
Marco GasiFreelancerCommented:
In my opinion, the best site to learn about regular expressions is RegExBuddy (http://www.regexbuddy.com/)

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.