Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


PHP Regular Expression Issue

Posted on 2012-08-17
Medium Priority
Last Modified: 2012-08-17
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)?

Question by:christamcc
  • 3
LVL 31

Accepted Solution

Marco Gasi earned 2000 total points
ID: 38306177
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);

Author Comment

ID: 38306237
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?
LVL 31

Expert Comment

by:Marco Gasi
ID: 38306280
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 '/'.

LVL 31

Expert Comment

by:Marco Gasi
ID: 38306284
In my opinion, the best site to learn about regular expressions is RegExBuddy (http://www.regexbuddy.com/)


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question