Quick Regular Expression Question
Posted: Sat Mar 27, 2010 2:41 pm
Greetings all!
I am looking to parse a regular expression of the following format:
[number with 1-5 characters][colon] ([number with 1-5 characters][semicolon][number with 1-2 characters][colon])* [colon]
ex: 16231:484;4:499;3::
I've been using this call to ereg: ereg("^([0-9]{1,5}):(([0-9]{1,5});([0-9]{1,2}):)*:", $q, $regs);
But my output isn't what I'm expecting, it seems to alternate between storing the whole string in the array, then the next variable. Is this due to my regular expression being incorrect, or am I simply using the function wrong? This is my first time using regular expressions, and I'm also kind of a PHP noob, just hoping someone can point me in the direction of my problem.
Sample output for: 16231:484;4::
$regs[0] = 16231:484;4::
$regs[1] = 16231
$regs[2] = 484;4:
$regs[3] = 484
$regs[4] = 4
I could just skip alternate array elements and hardcode the end, but that feels like too much of a hack and I suspect there's a better way. Thanks in advance for your responses!
I am looking to parse a regular expression of the following format:
[number with 1-5 characters][colon] ([number with 1-5 characters][semicolon][number with 1-2 characters][colon])* [colon]
ex: 16231:484;4:499;3::
I've been using this call to ereg: ereg("^([0-9]{1,5}):(([0-9]{1,5});([0-9]{1,2}):)*:", $q, $regs);
But my output isn't what I'm expecting, it seems to alternate between storing the whole string in the array, then the next variable. Is this due to my regular expression being incorrect, or am I simply using the function wrong? This is my first time using regular expressions, and I'm also kind of a PHP noob, just hoping someone can point me in the direction of my problem.
Sample output for: 16231:484;4::
$regs[0] = 16231:484;4::
$regs[1] = 16231
$regs[2] = 484;4:
$regs[3] = 484
$regs[4] = 4
I could just skip alternate array elements and hardcode the end, but that feels like too much of a hack and I suspect there's a better way. Thanks in advance for your responses!