Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
thatsme
Forum Commoner
Posts: 87 Joined: Sat Apr 07, 2007 2:18 am
Post
by thatsme » Wed Dec 05, 2007 3:20 am
How to check,
number-number-number-number-number-number
6 times numbers (any numbers (single, double, three digit etc)) followed by an hyphen, -
Code: Select all
if (preg_match ( "/^[0-9](-[0-9])*$/" , $str ) )
{
// match!
}
else
{
// doesn't match
}
it fails when i give 2, 3 digits.
arjan.top
Forum Contributor
Posts: 305 Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia
Post
by arjan.top » Wed Dec 05, 2007 4:25 am
If you have [0-9] it will match only one character/digit, you have to write something like that [0-9]+, it will match numbers with one or more digits
And If you have number-number-number-number-number-number all the time you may want to use {5}, so it wont match
number-number-number-number-number-number-number
Somethin like that:
thatsme
Forum Commoner
Posts: 87 Joined: Sat Apr 07, 2007 2:18 am
Post
by thatsme » Wed Dec 05, 2007 6:41 am
Thanks arjan.top,
i wanted the number any number of times and removed,
{5}
from
and tested using below code,
it fails even if i give, 6 numbers.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Wed Dec 05, 2007 7:29 am
In the second pattern you missed adding the plus sign after the repetitive subpattern;
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Wed Dec 05, 2007 8:11 am
vigge89 wrote: In the second pattern you missed adding the plus sign after the repetitive subpattern;
I think you're after the asterisk, not the plus.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Wed Dec 05, 2007 8:27 am
superdezign wrote: vigge89 wrote: In the second pattern you missed adding the plus sign after the repetitive subpattern;
I think you're after the asterisk, not the plus.
Ah yeah, my bad:
vapoorize
Forum Newbie
Posts: 22 Joined: Mon Dec 17, 2007 5:35 pm
Post
by vapoorize » Tue Jan 08, 2008 1:15 am
^ and $ are taken as string literals in preg if you don't add the m modifier, so it should be this if you mean to say the start and end of the line by those characters: