Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the # character seen as a 'word' character in Perl?

Tags:

regex

perl

Why is the # character seen as a 'word' character in Perl? Or am I misunderstanding how this code is supposed to work?

#!/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";

Yields:

Something_with_#_sign.jpg

I would have expected the # sign to have been replaced by an _ (underscore).

like image 450
Marcus Avatar asked Dec 25 '22 13:12

Marcus


1 Answers

/x doesn't modify the syntax of character classes (or of \x20, or of s{3,4}, etc, etc), so

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

is a weird way of writing

[^ "#:NOTacdefhilnorst\n\w]
like image 121
ikegami Avatar answered Dec 27 '22 11:12

ikegami