Page 1 of 1

[RegExp] Match /a(b|c)/ or /(b|c)a/

Posted: Sat Jan 17, 2009 6:38 am
by mmj
I currently am just writing the subpattern twice, e.g.

Code: Select all

/a(b|c)|(b|c)a/
Is it possible to do write this without putting the subpattern twice?

Something like pseudo-code: match a at ^ or $ of subpattern.

TIA

Edit: oops, never noticed this forum. Thanks admin

Re: [RegExp] Match /a(b|c)/ or /(b|c)a/

Posted: Mon Jan 19, 2009 2:47 pm
by GeertDD

Code: Select all

$part  = '(b|c)'; // by the way, better use [bc]
$regex = '/a'.$part.'|'.$part.'a/';

Re: [RegExp] Match /a(b|c)/ or /(b|c)a/

Posted: Mon Jan 19, 2009 4:54 pm
by mmj
Thanks for the tip. :)