1

Am trying to replace all `` with a HTML code tag

replace:

$string = "Foo `FooBar` Bar";

with:

$string = "Foo <code>FooBar</code> Bar";

i tried these

$pattern = '`(.*?)`';

my $replace = "<code/>$&</code>";
$subject =~ s/$pattern/$replace/im;

#And

$subject =~ s/$pattern/<code/>$&</code>/im;

but none of them works.

3
  • Mind the slashes. Commented Oct 11, 2016 at 17:25
  • 3
    Your string is in $string but you're doing the s/// on $subject. Could you show your actual code? And could you show what doesn't work? Commented Oct 11, 2016 at 17:27
  • 2
    Is that Markdown? If so, take a look at Text::Markdown. Commented Oct 11, 2016 at 17:32

1 Answer 1

4

Assuming you meant $string instead of $subject...

use strict;
use warnings;
use v5.10;

my $string = "Foo `FooBar` Bar";

my $pattern = '`(.*?)`';
my $replace = "<code/>$&</code>";

$string =~ s{$pattern}{$replace}im;
say $string;

This results in...

$ perl ~/tmp/test.plx
Use of uninitialized value $& in concatenation (.) or string at /Users/schwern/tmp/test.plx line 9.
Foo <code/></code> Bar

There's some problems here. First, $& means the string matched by the last match. That would be all of `FooBar`. You just want FooBar which is inside capturing parens. You get that with $1. See Extracting Matches in the Perl Regex Tutorial.

Second is $& and $1 are variables. If you put them in double quotes like $replace = "<code/>$&</code>" then Perl will immediately interpolate them. That means $replace is <code/></code>. This is where the warning comes from. If you want to use $1 it has to go directly into the replace.

Finally, when quoting regexes it's best to use qr{}. That does special regex quoting. It avoids all sorts of quoting issues.

Put it all together...

use strict;
use warnings;
use v5.10;

my $string = "Foo `FooBar` Bar";

my $pattern = qr{`(.*?)`};
$string =~ s{$pattern}{<code/>$1</code>}im;

say $string;
Sign up to request clarification or add additional context in comments.

4 Comments

is there any other way to use $1 without putting it directly to replace. like making something like $replace = '<code>$i</code>'
@ChrysUgwu Yes, but I don't recommend it because it's a security hole. If you use s{}{}e the right hand side will be eval'd as if it were code. But now you're vulnerable to code injection. It works like interpolation. my $foo = 23; my $bar = q[this $foo]; print "$bar" will say this $foo. But if you print eval qq["$bar"] you get this 23... but $bar could contain any code.
so Perl dosnt provide any secured pattern replacement like php? php.net/manual/en/function.preg-replace.php
@ChrysUgwu Search and replace is secure, unless you make it insecure. But it doesn't support expanding $1 in strings like PHP does in Example #1. There might be away to do it, but I don't know it. I'd suggest asking that as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.