[PHP] Returning part of a string, ereg..

2001-04-17 Thread Chad Day

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Returning part of a string, ereg..

2001-04-17 Thread Morgan Curley

try
$part = ereg("([0-9]{1})-([0-9]*)-", $f, $regs);

your original exp was looking for 1 digit followed by a dash follwed by 1 
digit followed by a dash

the * should match 0 or more occurences of the previous match ( in this 
case 0 or more numbers )

you shouldn't need the {1} in there either.

morgan

At 09:36 AM 4/17/2001 -0400, Chad Day wrote:
$part = ereg("([0-9]{1})-([0-9]{1,10})-", $f, $regs);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Returning part of a string, ereg..

2001-04-17 Thread Plutarck

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"" [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 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: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]