Re: escape character in regex

[email protected] (Ken Slater)
Newsgroups perl.beginners
Message-ID <CA+20Wcn5W2q-_+EFYeoB15N-3tkjR+5DzrquZWU-AjzSiTDHcg@mail.gmail.com>
On Mon, Oct 10, 2022 at 9:50 PM Henrik Park <[email protected]> wrote:
>
> John,
>
> for my this code:
>
> my $delimiter = '$';
> my $str = 'hello$world$buddy';
> my @li = split/$delimiter/,$str;
> print "@li","\n";
>
> How can I make it work correctly?
>
> Thanks
>
> John W. Krahn wrote:
> > "/" is NOT a special charater in a regular expression.  It is just that
> > in Perl the default delimiter for some operators is "/" (i.e. m//, s///,
> > tr///, etc.).
>
> --
> Simple Mail
> https://simplemail.co.in/
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>

John can probably explain it better than I can. But, I assume it has
to do with the fact that the dollar sign ($) is used as a special
character in Perl to indicate the start of a scalar variable.
Therefore, you need to escape the dollar sign ($) as follows:
      my $delimiter = '\$';
To indicate that you are using the dollar sign ($) as a character.
HTH, Ken
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.