From: PerlFAQ Server on
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

6.9: How can I quote a variable to use in a regex?

The Perl parser will expand $variable and @variable references in
regular expressions unless the delimiter is a single quote. Remember,
too, that the right-hand side of a "s///" substitution is considered a
double-quoted string (see perlop 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 = "Placido P. Octopus";
$regex = "P.";

$string =~ s/$regex/Polyp/;
# $string is now "Polypacido P. Octopus"

Because "." is special in regular expressions, and can match any single
character, the regex "P." here has matched the <Pl> in the original
string.

To escape the special meaning of ".", we use "\Q":

$string = "Placido P. Octopus";
$regex = "P.";

$string =~ s/\Q$regex/Polyp/;
# $string is now "Placido Polyp Octopus"

The use of "\Q" causes the <.> in the regex to be treated as a regular
character, so that "P." matches a "P" followed by a dot.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
From: Phred Phungus on
PerlFAQ Server wrote:
> This is an excerpt from the latest version perlfaq6.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is at http://faq.perl.org .
>
> --------------------------------------------------------------------
>
> 6.9: How can I quote a variable to use in a regex?
>
> The Perl parser will expand $variable and @variable references in
> regular expressions unless the delimiter is a single quote. Remember,
> too, that the right-hand side of a "s///" substitution is considered a
> double-quoted string (see perlop 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 = "Placido P. Octopus";
> $regex = "P.";
>
> $string =~ s/$regex/Polyp/;
> # $string is now "Polypacido P. Octopus"
>
> Because "." is special in regular expressions, and can match any single
> character, the regex "P." here has matched the <Pl> in the original
> string.
>

Am I then correct that a period is not a character?
--
fred
From: J�rgen Exner on
Phred Phungus <Phred(a)example.invalid> wrote:
>PerlFAQ Server wrote:
>> This is an excerpt from the latest version perlfaq6.pod, which
>> comes with the standard Perl distribution. These postings aim to
>> reduce the number of repeated questions as well as allow the community
>> to review and update the answers. The latest version of the complete
>> perlfaq is at http://faq.perl.org .
>>
>> --------------------------------------------------------------------
>>
>> 6.9: How can I quote a variable to use in a regex?
>>
>> The Perl parser will expand $variable and @variable references in
>> regular expressions unless the delimiter is a single quote. Remember,
>> too, that the right-hand side of a "s///" substitution is considered a
>> double-quoted string (see perlop 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 = "Placido P. Octopus";
>> $regex = "P.";
>>
>> $string =~ s/$regex/Polyp/;
>> # $string is now "Polypacido P. Octopus"
>>
>> Because "." is special in regular expressions, and can match any single
>> character, the regex "P." here has matched the <Pl> in the original
>> string.
>>
>
>Am I then correct that a period is not a character?

???
How did you come to that conclusion?

jue
From: Peter J. Holzer on
On 2010-03-28 08:47, Phred Phungus <Phred(a)example.invalid> wrote:
> PerlFAQ Server wrote:
>> $string = "Placido P. Octopus";
>> $regex = "P.";
>>
>> $string =~ s/$regex/Polyp/;
>> # $string is now "Polypacido P. Octopus"
>>
>> Because "." is special in regular expressions, and can match any single
>> character, the regex "P." here has matched the <Pl> in the original
>> string.
>>
>
> Am I then correct that a period is not a character?

No. Why do you think so?

hp
From: Steve M on
On 3/28/2010 1:47 AM, Phred Phungus wrote:
> PerlFAQ Server wrote:
>> This is an excerpt from the latest version perlfaq6.pod, which
>> comes with the standard Perl distribution. These postings aim to
>> reduce the number of repeated questions as well as allow the community
>> to review and update the answers. The latest version of the complete
>> perlfaq is at http://faq.perl.org .
>>
>> --------------------------------------------------------------------
>>
>> 6.9: How can I quote a variable to use in a regex?
>>
>> The Perl parser will expand $variable and @variable references in
>> regular expressions unless the delimiter is a single quote. Remember,
>> too, that the right-hand side of a "s///" substitution is considered a
>> double-quoted string (see perlop 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 = "Placido P. Octopus";
>> $regex = "P.";
>>
>> $string =~ s/$regex/Polyp/;
>> # $string is now "Polypacido P. Octopus"
>>
>> Because "." is special in regular expressions, and can match any single
>> character, the regex "P." here has matched the <Pl> in the original
>> string.
>>
>
> Am I then correct that a period is not a character?

Ah.. confusion aside, no.

'.' is a metacharacter, which (as it says) means it has a special
meaning inside a regex.

To access a literal '.' you need to backslash it, same as the
\|()[{^*+? meta characters..

So.

/./ matches any single character

/\./ matches a period.

/.{3}/ matches exactly three characters, but ANY three, and so on.

hth

\s

--
"There is no use in your walking five miles to fish when you can depend
on being just as unsuccessful near home." M. Twain