Page 1 of 1
matching optional text
Posted: Sat May 19, 2007 9:29 am
by GeXus
I'm using the following regex to match some text..
/<td class="a">(.*?)<br/'
In some instances, I need to match <td class="a bc"
So is there a way to have an optional match for the (space)bc ?
Thanks!
Posted: Sat May 19, 2007 9:31 am
by feyd
yes.
Posted: Sat May 19, 2007 9:32 am
by GeXus
Just to add I tried this
but that didnt work
Posted: Sat May 19, 2007 9:35 am
by GeXus
feyd wrote:yes.
Sweet.
c'mon.....

Posted: Sat May 19, 2007 10:00 am
by feyd
Come on what? You asked if it was possible.
Posted: Sat May 19, 2007 10:09 am
by GeXus
Ok fair enough... I retract statement. Can anyone SHOW me how to do this, through the use of code.
Thank you

Posted: Sat May 19, 2007 10:18 am
by feyd
What you tried should work...
Code: Select all
[feyd@home]>php -r "echo preg_match('#<td class=\"a( bc)?\">(.*?)<br#is','<td class=\"a bc\">blahblah<br/>');"
1
Posted: Sat May 19, 2007 3:15 pm
by GeXus
Your right it worked! It was a different array key when it gets the optional.. didnt see that. Thanks!