What does \ D do in Perl regular expressions

In some code that I support, I found an expression:

$r->{DISPLAY} =~ s/\Device//s; 

What surprises me is that it matches both the device and the device!

I did not find mention of \ D in the documentation, only \ d.

Can someone clarify please ...

+8
regex perl
source share
3 answers

\D is a negation of \D , that is, it matches anything that is not a number.

+19
source share

In this regex, \D looks like a typo. It works for both d and d , only because it matches any character that is not a digit (0-9).

A more suitable regular expression (if the goal is to match "device" or "device"):

 s/(d|D)evice// # one way s/[dD]evice// # another way 

The s parameter is also a bit odd. From perldoc perlop

 s Treat string as single line. (Make . match a newline) 

And in this line there is no such coincidence.

+11
source share

You already have an answer. However, there is documentation in perldoc perlrecharclass about this. See Backslash Sequence Information.

He also mentioned in perldoc perlrequick and in regular perldoc perlretut in the section Using character classes. However, in these two sections he is rather buried.

+2
source share

All Articles