Current location: Hot Scripts Forums » Programming Languages » PHP » regular expression


regular expression

Reply
  #1 (permalink)  
Old 06-26-09, 03:13 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
regular expression

Hi there..

I got a form where want to check if a zipcode is correct..

A zipcode should look like 0000AA or 0000 AA

I made this patern:
PHP Code:

$patroon3 "[0-9]{4}+[A-Za-z]{2}"
and check ik like this:

PHP Code:

if(!eregi($patroon3$postcode2))) 

But it's isn't working correct..

where did I go wrong?.

_j
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-26-09, 03:34 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

'~^\d{4}[ ]?[a-z]{2}$~i' 

And get used to preg_match() rather than the ereg* functions. They're way slower and they're gonna be disabled by default (or removed completely, not sure) in PHP 6.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-26-09, 04:03 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
thnx nico..

can you explain the expression a bit?.
this is one of the part I'm still kind of new to
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-26-09, 04:16 AM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
This is a good tutorial:
PHP Video Tutorials - Regular Expressions

Nico gave it to me some time ago hehe.
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-26-09, 04:36 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
^ matches the beginning of the string.
\d matches a digit, {4} times.
[ ]? matches an optional space.
[a-z] matches characters from a-z, {2} times.
$ matches the end of the string.
i, makes the whole thing case-insensitive.

Take a look at that video too. It's pretty nice.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-14-09, 04:41 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
What stands a '~' for in a regular expression?.

'\s*' means a possible space right?.
and []? a possible space?.

Is there a site where to find a list of options to use i a regular expression?.

PHP Code:

$pattern2 "~(<div\s*.?>)~"// seems to me to search for a div opening en div closing tag
$pattern3 "~(<span\s*.?>)~"// seems to me to search for a span opening en span closing tag 

_j

Last edited by jonnekke; 07-14-09 at 04:44 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-14-09, 05:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
The ~ are delimiters. I think (if I remember correctly) the guy in the video uses dollar signs as delimiters. You can use pretty much anything. If you haven't watched the video yet, I stronly encourage you to do so. It will answer most of your questions.

\s* means 0 (zero) spaces or any amount.
[ ]? is a possible space, yes.

Quote:
Originally Posted by jonnekke View Post
Is there a site where to find a list of options to use i a regular expression?.
Yes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Regular Expression coolsaggu PHP 3 09-29-08 07:25 AM
PHP regular expression problem coolsaggu PHP 0 08-08-08 03:58 AM
Regular Expression in C# - The Basic lartzi ASP.NET 1 06-28-08 02:31 AM
Regular Expression mohit Everything Java 9 05-08-08 05:55 AM
Help with regular expression... :) eddyvlad PHP 2 01-08-06 11:10 PM


All times are GMT -5. The time now is 06:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.