开发者

How to use the chr function inside a regular expression in Perl?

I have a Perl script which has a string which may or may not contain character 146 in开发者_开发知识库 the ASCII character codes. I'm interested in using the chr() function inside a regular expression to find out if that string really does contain that character. If the character is there, I would like to substitute it with a "'" character.

Is this possible? If not what's another way to do this?

This one has my stumped. Thanks!


You can substitute variables into patterns:

$c = chr(146);
$target =~ s/$c/'/go; # "o" means $c won't change so remember it

but usually you would specify it as a literal by converting it to octal or hex:

$target =~ s/\222/'/g; # octal
$target =~ s/\x92/'/g; # hex
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜