Page 1 of 1

PHP and regular expression help

Posted: Thu Aug 05, 2004 12:39 pm
by flamesburn
Hello,
Im extremely new to the world of regular expressions and need to create one for a 10 digit phone number that can have spaces dashes and brackets and a postal code that can have 6 characters and be seperated with paces or dashes. does anyone have a good site to read up on regular expressions or kind of point me in the right direction>

thanks in advance

JH

Posted: Thu Aug 05, 2004 12:44 pm
by nigma
Here are a few tutorials that I found, but haven't tried, so can't guarentee quality.

http://zend.com/zend/tut/tutorial-delin2.php
http://weblogtoolscollection.com/regex/regex.php
http://www.phpbuilder.com/columns/dario19990616.php3
http://www.phpfreaks.com/tutorials/52/0.php

I've been real pleased with some of the tutorials that i've found at zend.com so i'd probably try the first tutorial first :)

Posted: Thu Aug 05, 2004 1:48 pm
by McGruff
This program http://www.weitz.de/regex-coach/#install is very useful to test out expressions.

thanks

Posted: Thu Aug 05, 2004 2:01 pm
by flamesburn
thanks for the help, the articles really help and I can definately see how that program comes in helpful. Im just really new at regular expressions.

im trying to validate a postal code i.e. N2R5A9

but I want to be able to have optional spaces and dashes for example

N2R5A9
N2R-5A9
N2R 5A9

should all validate correctly.

the pattern i have come up with does not allow for optional spaces and dashes .......
$pattern ="^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]";

any thoughts? im sure its easy once i get a grasp at how to use optional characters.

Posted: Thu Aug 05, 2004 2:10 pm
by Weirdan

Code: Select all

$pattern = "^\w[ -]?\d[ -]?\w[ -]?\d[ -]?\w[ -]?\d$"
it's for PCRE (preg_*) regexp functions

hmmm

Posted: Thu Aug 05, 2004 2:36 pm
by flamesburn
im using ereg

is there anyway to stay along the lines of the original formatting
$pattern ="^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]";

just add in a couple things?

thanks for the help

Posted: Thu Aug 05, 2004 3:30 pm
by feyd
*pssst* Weirdan, that pattern doesn't have starting and ending delimiters for PCRE.. ;) unless I'm thinking of something else.. :)

Posted: Thu Aug 05, 2004 5:52 pm
by Weirdan
feyd wrote:*pssst* Weirdan, that pattern doesn't have starting and ending delimiters for PCRE.. ;) unless I'm thinking of something else.. :)
Yup.
Too much JS-ness last days.... Must have been tired....