Re: Returning part of a string, ereg..
| From: | Plutarck | Date: | Tue, 17 Apr 2001 15:04:58 +0000 |
| Subject: | Re: Returning part of a string, ereg.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-48979@lists.php.net to get a copy of this message | ||
This:
ereg("([0-9]{1})-([0-9])-", $f, $regs);
What that means is "Capture a single occurance of 0-9, then a hyphen, then
capture a single occurance of 0-9 if it is imediately folowing by a hyphen."
Try this instead:
([0-9]{1})-([0-9]+)-
The "+" means "one or more". So "[0-9]+" is the same as
"[0-9]{1,}". For
instance, "{x,y}" matches at least x amount of characters, but no more than
y amount of characters. Leave y off to match "x or more of the mentioned
characters". But don't forget that comma.
Regex is incredibly hard until you learn it. Then it doesn't seem hard
anymore :)
--
Plutarck
Should be working on something...
...but forgot what it was.
""Chad Day"" <cday@atpco.com> wrote in message
news:NFBBJEHDJOLEGDCDKEGGAEFACBAA.cday@atpco.com...
> I'm horrible at regex's, maybe someone can help me out.
>
> What I'm trying to do is parse a string in the format:
number-number-number
> (ex. 1-23123-312039128)
>
> I need to pull that second number, regardless of length
>
> This code returns nothing:
>
> $part = ereg("([0-9]{1})-([0-9])-", $f, $regs);
>
> but
>
> $part = ereg("([0-9]{1})-([0-9]{1,10})-", $f, $regs);
>
> will return the number, but I don't want to take a chance and limit it to
10
> characters. I thought the first bit of code would work.. any ideas what
is
> wrong?
>
> Thanks,
> Chad
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>