I'm not sure why you added the update. I don't see what it adds, and it's not true. /\bChi_2\b/ will match plenty of strings.
'Chi_2' =~ /\bChi_2\b/ # Match '!Chi_2!' =~ /\bChi_2\b/ # Match
Maybe you had a specific string in mind, but I don't see how this relates to the OP. He would not use Chi_2 in the regex pattern.
In a world where an identifier matches /^\w+\z/, you might do something like
($_ = '\\Chi+3' ) =~ s/\\Ch\b/$ch/g; # Won't replace ($_ = '\\Chi+3' ) =~ s/\\Chi\b/$chi/g; # Will replace ($_ = '\\Chi_2+3') =~ s/\\Chi\b/$chi/g; # Won't replace
But what if identifiers match /^[a-zA-Z]\z/? You'd want the following behaviour:
($_ = '\\Chi+3' ) =~ s/\\Ch???/$ch/g; # Won't replace ($_ = '\\Chi+3' ) =~ s/\\Chi???/$chi/g; # Will replace ($_ = '\\Chi_2+3') =~ s/\\Chi???/$chi/g; # Will replace
That's the OP's question.
As I've already mentioned, I recommend extracting the identifier, then checking if it's one of interest. This can be as simple as the following:
/\\([a-zA-Z]+)/ exists($vars{$1}) ? $vars{$1} : "\\$1" /eg
The technique scales well, and it avoids the problem of matching something you've previously replaced.
In reply to Re^2: Defining Characters in Word Boundary?
by ikegami
in thread Defining Characters in Word Boundary?
by iaw4
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |