Regex: How could I avoid a character class?

Is it possible to write this without [^...], but using \P{...}?

#!/usr/bin/env perl 
use warnings;
use 5.012;
use utf8;

my $string = '_${Hello}?${World}!';

$string =~ s/[^\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}]/-/g; 

say "<$string>";
+3
source share
1 answer

Well, it’s possible, but I don’t think I would call it an improvement:

#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;

my $string = '_${Hello}?${World}!';

$string =~ s/(?=\P{Alphabetic})
             (?=\P{Mark})
             (?=\P{Decimal_Number})
             (?=\P{Connector_Punctuation}) . /-/xgs;

say "<$string>";

With a few positive looks, they all must succeed. Thus, it matches a single character ( .), which is not alphabetic, not Mark, not Decimal_Number, not Connector_Punctuation, like a negative character class.

I added a modifier /sbecause the original regular expression will match a new line (although there is no pattern in your line). I added /xto add some spaces and break them into several lines.

, ?

+6

All Articles