为什么#字符被视为'字' Perl中的角色?

时间:2015-09-29 17:54:01

标签: regex perl

为什么#字符被视为'字' Perl中的角色?或者我误解了这段代码应该如何工作?

#!/usr/bin/perl

my $filename = "Something_with_#_sign.jpg";

$filename =~ s/        # substitute...
                [^             # characters which are NOT:
                \w                # "word" characters
                ]              # end of character classes
                /_/xg;     # ...with an underscore

print "$filename\n";

收率:

Something_with_#_sign.jpg

我原本预计#符号会被_(下划线)替换。

1 个答案:

答案 0 :(得分:10)

/x不会修改字符类(或\x20s{3,4}等)的语法,所以

[^             # characters which are NOT:
\w                # "word" characters
]              # end of character classes

是一种奇怪的写作方式

[^ "#:NOTacdefhilnorst\n\w]