Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex expression using PHP preg_match 1

Status
Not open for further replies.

webslinga

Programmer
Jun 20, 2006
55
US
Hey all,
I need to check for a string and see if it matches EXACTLY three digits from 0 - 9.

I am thinking that it will be something like this:
/d{3} OR [0-9]{3} but it simply does not work. Please let me know what I am doing wrong. Thanks.
 
Either of

\d{3}

[0-9]{3}

should work to match three digits in succession. But what do you mean by "EXACTLY"?

If you mean "The string should exist entirely of three digits", then the above strings won't work, as they will, for example, match "0000", since a string of four digits certainly includes a string of three digits. To match all strings that exist in their entireties as only three digits, try:

^\d{3}$

or

^[0-9]{3}$



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks sleipnir,
^[0-9]{3}$ worked for me. And you were correct in assuming that I wanted to match all strings that exist in their entireties as only three digits. Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top