siraj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I want to replace all the occurrences of \* by STAR in a string with a simple regex. I am not able to do it, this is my code that I tried:


$value = "sample1\*sample2\*sample3*sample4*sample5";
$value =~ s#\*#STAR#g;
print "$value";

output: sample1STARsample2STARsample3STARsample4STARsample5

However it replaces all the occurrences of *.

Could someone help me.........

Thanks Guys, I understootd the problem, the way I gave the input is wrong, I gave \* in double quoted string and try to match it in the variable $value.

Replies are listed 'Best First'.
Re: Regex problem
by ikegami (Patriarch) on Aug 02, 2007 at 15:23 UTC

    \ and * are special characters in regexp. (\ is actually special in all literals except heredocs.) You need to escape them by preceeding them with a \.

    $value =~ s#\\\*#STAR#g;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Regex problem
by Fletch (Bishop) on Aug 02, 2007 at 15:24 UTC

    Both \ and * are special in a regular expression, which means that to match them literally you have to use a backslash to escape them. What you've done is just escape the special meaning of the *; you need s#\\\*#STAR#g to match the literal characters \* (the first \\ matches a single backslash, then the \* matches a single *). Read perlretut and perlre for more details.

Re: Regex problem
by toolic (Bishop) on Aug 02, 2007 at 15:25 UTC
    Is this the output you are looking for?
    sample1STARsample2STARsample3*sample4*sample5
    Here is what I used to generate this:
    #!/usr/bin/env perl use warnings; use strict; my $value = 'sample1\*sample2\*sample3*sample4*sample5'; $value =~ s#\\\*#STAR#g; print "$value\n";
    Note the escaped backslash in the regexp.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Regex problem
by FunkyMonk (Bishop) on Aug 02, 2007 at 15:24 UTC
    If I understand your question correctly, the problem isn't with your s///, it's with your initialisation of $value. It should be single-quoted, not double.
    my $value = 'sample1\*sample2\*sample3*sample4*sample5'; $value =~ s#\*#STAR#g; print $value

    Output:

    sample1\STARsample2\STARsample3STARsample4STARsample5

      FunkyMonk:

      I think you meant:

      my $value = 'sample1\\*sample2\\*sample3\\*sample4\\*sample5'; print $value,"\n"; $value =~ s#\\\*#STAR#g; print $value,"\n";

      as the \ is a special character and needs to be quoted in strings and regexes. The * is special in the regex and also needs quoting.

      ...roboticus

      UPDATE: ikegami just pointed out to me that I am a doofus incorrect, as demonstrated:

      my $value = 'sample1\\*sample2\\*sample3\\*sample4\\*sample5'; my $value2 = 'sample1\*sample2\*sample3\*sample4\*sample5'; if ($value eq $value2) { print "I am a studly programmer\n"; } else { print "I am a doofus\n"; }

        In single-quoted literals, escaping \ is optional if it's not followed by \ or the string delimiter.

        $x = 'a\b'; print("$x\n"); $x = 'a\\b'; print("$x\n");
        a\b a\b
Re: Regex problem
by chakram88 (Pilgrim) on Aug 02, 2007 at 15:30 UTC
    You are close.

    Remember, in a regex *all* the special characters need to be escaped if you are trying to match them explicitly.

    Take a look at the perl regex tutorial (perldoc perlretut) -- the information to help you is metacharacters.

    Or check out the tutorials here. What you're doing is rather basic, and has been answered before. RegEx Tutorials