Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 20th, 2008, 04:33 AM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,490 srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 19 h 21 m 45 sec
Reputation Power: 831
Php, difference between \d and [\d]

Hello,
I have this regex to check for the existence of digits in a given string. It may or may not contain anything else, but is should have one or more digits.
Here is the code
php Code:
Original - php Code
  1. if (preg_match('/\d+/', $val))
  2.     echo "$val contains digits\n";
  3. else
  4.     echo "$val does not contain digits\n";

php Code:
Original - php Code
  1. if (preg_match('/[\d]+/', $val))
  2.     echo "$val contains digits\n";
  3. else
  4.     echo "$val does not contain digits\n";


Both code snippets give same results. So, what is the difference between \d and [\d].

Thank you.
__________________
Why do we always seek someone, something or some thought? Are we afraid of ourselves?

Reply With Quote
  #2  
Old August 20th, 2008, 05:03 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 14 m 53 sec
Reputation Power: 3885
There's no difference. Using [ ... ] allows you to create a character class which will match any of the characters contained within it. So for example if you wanted to match 'a', 'b' or 'c', you'd do:
Code:
[abc]

There are also pre-created character classes (\w for 'word characters', \d for digits, etc). So these two are equivalent:
Code:
[0123456789]
\d

Your square bracket character classes can contain these predefined ones too, so if for example you wanted to match numeric digits or whitespace, you could do:
Code:
[\d\s]

Here, because you're only using one predefined character class, the square brackets are unnecessary.

Reply With Quote
  #3  
Old August 20th, 2008, 05:34 AM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,490 srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 19 h 21 m 45 sec
Reputation Power: 831
Thanks.
Some notes for you:
I repped you but the message is not visible. When I tried to rep you again it said, 'You cannot rep the same post twice'.
It would be better if the resources and suggestions threads are stickied. Otherwise they will be go into the last page.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Php, difference between \d and [\d]

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap