From: Mark-Jason Dominus Date: Mon, 1 Sep 2003 16:19:20 +0000 (-0400) Subject: Explain \Q better X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c83084d1250c5e3fe3236f495fb04a079ccb34d8;p=p5sagit%2Fp5-mst-13.2.git Explain \Q better Message-ID: <20030901201920.3929.qmail@plover.com> p4raw-id: //depot/perl@20997 --- diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index 9bbf80a..0a134c3 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -292,14 +292,26 @@ a double-quoted string (see L for more details). Remember also that any regex special characters will be acted on unless you precede the substitution with \Q. Here's an example: - $string = "to die?"; - $lhs = "die?"; - $rhs = "sleep, no more"; + $string = "Placido P. Octopus"; + $regex = "P."; - $string =~ s/\Q$lhs/$rhs/; - # $string is now "to sleep no more" + $string =~ s/$regex/Polyp/; + # $string is now "Polypacido P. Octopus" -Without the \Q, the regex would also spuriously match "di". +Because C<.> is special in regular expressions, and can match any +single character, the regex C here has matched the in the +original string. + +To escape the special meaning of C<.>, we use C<\Q>: + + $string = "Placido P. Octopus"; + $regex = "P."; + + $string =~ s/\Q$regex/Polyp/; + # $string is now "Placido Polyp Octopus" + +The use of C<\Q> causes the <.> in the regex to be treated as a +regular character, so that C matches a C

followed by a dot. =head2 What is C really for?