The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Php, difference between \d and [\d]
Discuss Php, difference between \d and [\d] in the Regex Programming forum on Dev Shed. Php, difference between \d and [\d] Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 20th, 2008, 04:33 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
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 |
|
|
|
echo "$val contains digits\n"; else echo "$val does not contain digits\n";
php Code:
Original
- php Code |
|
|
|
echo "$val contains digits\n"; else 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?
|

August 20th, 2008, 05:03 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
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:
There are also pre-created character classes (\w for 'word characters', \d for digits, etc). So these two are equivalent:
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:
Here, because you're only using one predefined character class, the square brackets are unnecessary.
|

August 20th, 2008, 05:34 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|