preg_match() help<

liunx

Guest
Ok, I read the php.net page on preg_match (<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.preg-match.php">http://www.php.net/manual/en/function.preg-match.php</a><!-- m -->), but I still don't think I fully get it. It seems to not explin most of the slash commands for it. For example, in:
/* The \b in the pattern indicates a word boundary, so only the distinct
* word "web" is matched, and not a word partial like "webbing" or "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

It tells about the \b command, but what is the /i, and I see other things...like a /^ etc. I personally use this (are of scoutt) on my site:
preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', $_POST['email']
to see if an E-Mail is in a valid format. It works but I have no idea WHY it works. I want to use the same basic idea to force a phone # to be xxx-xxx-xxxx or (xxx)xxx-xxxx or something, but I have no idea how to do so. Does anyone know of any better tutorials etc on preg_match()?for one you can't use the
PHP:
 tags for regular expressions. this board parses the extra slashes in there.

for learning regular expressions is as simple as looking at perl's expressions. they are pretty close to the same. the manual leaves a lot to be desired when it comes to the regular expressions. look into <!-- w --><a class="postlink" href="http://www.perldoc.org">www.perldoc.org</a><!-- w --> or my site.
\i is for case-insentivie mathces, /^ is for matching exact

everyone I have talked to says it took them a while to grasp regexp. jsu tleep playing with it and it will come to you. I have to go back and read up on it everytime I use it.yah, this confused me for a while also.  I got the point in my book where it was talking about this.  I think I got the basic concept now.

preg_match('/^[-!#$%&'*+./0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+.)+([0-9A-Z]){2,4}$/i', $_POST['email']

for this email address: <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->
so if i'm not mistaken it makes sure there's none of those weird symbols, and makes sure it's just 0-9 or a-z for the bob part of the email, and then it looks for an @ sign, and then it makes sure there's only 0-9 or a-z in the domain, and then it looks for a period(or more), and once again checks for 0-9 or a-z.  I'm not sure what this part does though: {2,4}$/ithat SHOULD read like this:
preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', $_POST['email'])For Josh:

([0-9A-Z]){2,4}$/i

([0-9A-Z])
Match 0-9 or A-Z, group the results.

{2,4}
Match only if there are 2 to 4 of the above characters.

$
Make sure this group of characters is at the end of the string.

/i
Make this a case-insensitive match, meaning A-Z also matches a-z.


Recent thread: <!-- m --><a class="postlink" href="http://www.htmlforums.com/showthread.php?s=&threadid=35790Thanks">http://www.htmlforums.com/showthread.ph ... 5790Thanks</a><!-- m --> rydberg :)

But I don't see any part that allows for an address like <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e --> posted by scoutt 
\i is for case-insentivie mathces, /^ is for matching exact 

^ matches the start of the string, like $ matches the end of the string. ^hello$ matches the string "hello" only. "hello " will fail because of the white character.Originally posted by Josh 
Thanks rydberg :)

But I don't see any part that allows for an address like <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->  
U B welcome;

@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}

This will allow for this pattern:

@any.number.of.subdomains.domain.com

which is no different from

@example.co.uk

The plus sign after the first group(first set of parenthesis) makes this possible.

([-0-9A-Z]+\.)+

[-0-9A-Z]+\.
Match '-', 0-9 or A-Z, one of more times, followed by a period.

(...)+
Match one or more set of the characters above, make each set a group.So one for Aaron:

/^[-!#$%&'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i

/^
Match the start of the string.

[-!#$%&'*+\\./0-9=?A-Z^_`{|}~]+
Match these characters: -!#$%&'*+\./=?^_`{|}~ as well as 0-9 and A-Z. All of these are enclosed in brackets, which make all of this into what the regex engine considers one type of character, or character class. The plus sign after the brackets means that any of these characters in the character class may occur one or more times, and they must be at the start of the string, that's what /^ does.

The rest is pretty much described in my previous posts, I think... Let me know if you have any questions. This is an OK regex for matching an e-mail address, it's not perfect. A perfect one involves a lot of work, and in most situations, it's simply not worth all the hassle and CPU.ok...first off, I found a fairly good site to read: <!-- m --><a class="postlink" href="http://dinki.mine.nu/word/regex/regex.php">http://dinki.mine.nu/word/regex/regex.php</a><!-- m -->

Also, I have the E-Mail one (stolen :), but working...and now I understand it!)

I needed to verify a phone # was in the format XXX-XXX-XXXX and I think I did that with the following: preg_match('/^[1-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}/', $_POST['d_phone'])

that way, the first digit of the area code cannot be a 0, and the rest can be 0-9Originally posted by AaronCampbell 
preg_match('/^[1-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}/', $_POST['d_phone'])

Yeah that looks quite good. I suggest a few changes... The important one is the $ at the end of the expression, to make sure there's not any junk at the end of the number. Also, I think you can use \d instead of [0-9]. There is no difference, but it's shorter. It's been a while since I did regex in PHP, but I'm pretty sure it works.

preg_match('/^[1-9]{1}\d{2}-\d{3}-\d{4}$/', $_POST['d_phone'])thanks alot rydberg, i think i got it figured out for the most part :)Originally posted by Rydberg 
Also, I think you can use \d instead of [0-9]. There is no difference, but it's shorter. It's been a while since I did regex in PHP, but I'm pretty sure it works.


I don't know why, because it looks to me like \d is any digit, but it doesn't work...it'll let 1 work as a phone number.  I added the $ at the end though, and that works great.  I'll sacrifice a couple extra characters in my code for now, and try to figure out the \d thing later :)\d is for 1 digit character

eg: 
/\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format

nevermind, I see {2} after it so it should have worked.Originally posted by AaronCampbell 
I don't know why, because it looks to me like \d is any digit, but it doesn't work...it'll let 1 work as a phone number.  I added the $ at the end though, and that works great.  I'll sacrifice a couple extra characters in my code for now, and try to figure out the \d thing later :)  

That's very surprising... Unfortunately I'm not in a position to run any tests now, but I'll get to it some day.preg_match('/(\d{1}|\d{3})-\d{3}-\d{4}$/'

works for me on numbers like so

x-xxx-xxx-xxxx
xxx-xxx-xxxx

not sure what format you wanted them inOriginally posted by scoutt 
preg_match('/(\d{1}|\d{3})-\d{3}-\d{4}$/'

works for me on numbers like so

x-xxx-xxx-xxxx
xxx-xxx-xxxx

not sure what format you wanted them in 

The problem is, that also works for x-xxx-xxxx

It did however, inspire me to try harder :)  Here is what I ended up with:
preg_match('/^(1-[1-9]{1}\d{2}|[1-9]{1}\d{2})-\d{3}-\d{4}$/', $_POST['pu_phone'])

that should allow: 1-YXX-XXX-XXXX and YXX-XXX-XXXX where X is any digit, and Y is 1-9 (0 is not a valid first digit).I'd recommend this:
preg_match('/^(1-)?[1-9]\d{2}-\d{3}-\d{4}$/', $_POST['pu_phone'])

(1-)? allows for 0 or 1 occurance of "1-", and there's no need for {1}. :)works like a charm...I hadn't learned about the ? yet, but I had realized that I didn't need the {1}!  Anyway...thank you guys for all your help...it's pretty easy once you get it, but learning from an example with no explaination is almost impossible with this one!
 
Back
Top