Perl Regex Replace Double Quotes With Single Quotes
Clash Royale CLAN TAG#URR8PPP
Perl Regex Replace Double Quotes With Single Quotes
I have a long string which looks like this:
"Key1:Value1,
Key2:value2,
Key3:namespace="randomName",
Key4:Some other value,
Key5:namespace="anotherRandomName"
...
"
I want to replace namespace="randomName"
with namespace='randomName'
(just from double quotes to single quotes).
namespace="randomName"
namespace='randomName'
What's the best way to do it with perl regex?
s/(:namespace=)"([^"]*)"/$1'$2'/
What have you tried? What problems are you having? Please show us your code. Stack Overflow is not a code-writing service (although I see that two people have already decided to be generous and write code for you!)
– Dave Cross
Aug 6 at 15:24
1 Answer
1
use strict;
use warnings;
my $str = <<EOD;
Key1:Value1,
Key2:value2,
Key3:namespace="randomName",
Key4:Some other value,
Key5:namespace="anotherRandomName"
EOD
$str =~ s/bnamespace=K"([^"]+)"/'$1'/g;
print $str;
Output:
Key1:Value1,
Key2:value2,
Key3:namespace='randomName',
Key4:Some other value,
Key5:namespace='anotherRandomName'
@downvoter: May I know why?
– Toto
Aug 7 at 7:48
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
s/(:namespace=)"([^"]*)"/$1'$2'/
– anubhava
Aug 6 at 14:51