matching optional text

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

matching optional text

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Just to add I tried this

Code: Select all

/<td class="a( bc)?">(.*?)<br/'
but that didnt work
Last edited by GeXus on Sat May 19, 2007 9:35 am, edited 1 time in total.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

feyd wrote:yes.

Sweet.


c'mon..... :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Come on what? You asked if it was possible.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Ok fair enough... I retract statement. Can anyone SHOW me how to do this, through the use of code.

Thank you :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Your right it worked! It was a different array key when it gets the optional.. didnt see that. Thanks!
Post Reply