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

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

Moderator: General Moderators

Post Reply
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

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

Post 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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Post by GeertDD »

Code: Select all

$part  = '(b|c)'; // by the way, better use [bc]
$regex = '/a'.$part.'|'.$part.'a/';
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

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

Post by mmj »

Thanks for the tip. :)
Post Reply