PS: this simpler pattern did not work: /\[([@=])(.*?)($pat)(.*?)\1\]/ because in string with $pat = xxx like [@abc@] xxx [@def@] it would match the lot, rather than failing because $pat is not in either of the two [@....@] markup brackets. so I am scratching my head... Hans