How to use preg_replace - preg-replace

I have a String:
$str= "(and marque:'Mercedes Benz' model:'F Type')";
I want to return string like this:
$str2= "(and marque:'Mercedes-Benz' model:'F-Type')";
so, I want to replace space with '-' char but only between '' chars.
Should I use preg_replace function?

How about:
$str= "(and marque:'Mercedes Benz' model:'F Type')";
$str = preg_replace("/'(\w+)\s+/","'$1-", $str);
echo $str,"\n";
output:
(and marque:'Mercedes-Benz' model:'F-Type')

Related

Preg_replace in php - multiple replace

I have $string, which contains:
this example
and I have these 3 expressions:
$pattern = array('/aa*/','/ii*/');
$replacement = array('<i>$0</i>','<b>$0</b>');
preg_replace($pattern, $replacement, $string);
where, preg_replace returns:
th<b>i</b>s ex<<b>i</b>>a</<b>i</b>>mple
and I need output like this:
th<b>i</b>s ex<i>a</i>mple
which means, that I want to replace only characters in original string. Is it possible?
This does the trick in my testing
$pattern = array('/([a-z|^|\s])(aa*)/', '/([a-z|^|\s])(ii*)/');
$replacement = array('$1<i>$2</i>','$1<b>$2</b>');

How to print special char in Perl

I have a special char in my variable like this
my $var = "\n";
my $var_tab = "\t";
I need to print out not this character but character value/code (\t,\n).
I have tried to add one more slash, but it doesn't work.
my $var.="\\";
How can I print value of special char?
It might help us understand the context if you explained why you want this and what you're trying to achieve.
If you simple want to dump out the value of a variable and have the special characters be visible and readable then perhaps Data::Dumper would help:
use Data::Dumper
$var = "\n";
$var_tab = "\t";
$Data::Dumper::Terse = 1;
$Data::Dumper::Useqq = 1;
print Data::Dumper::qquote($var); # "\n"
print Data::Dumper::qquote($var_tab); # "\t"
If you want print the special character value, Try regex for substitute the \ with empty.Like
my $var = "\\n";
$var =~s/\\//g;
print "$'";
Else If you want print the \n . Just use the single quotes instead of double quotes. or use forward slashes within double quotes.
my $var = "\\n"; #slashes for escape the special character
my $var_tab = '\t'; #single quotes also escape the special character
print "$var $var_tab ";

How to replace a $ in a string in perl script

I have a string,
$str = abc#$and#def
I tried to replace '$' with it's hex value using ,
$str=~s/$/%26/g
But the output is abc#.
This might be because '$'is considered as the end of the line or string.
Please let me know
Your problem is nothing to do with your substitution; when you are assigning to the string in the first place:
$str = "abc#$and#def";
$and and #def are treated as variables to interpolate.
You need to escape the sigils or use single quotes (which don't interpolate variables):
$str = 'abc#$and#def';
# or
$str = "abc#\$and\#def";
And you really really need to enable warnings, which would have told you your assignment was the problem.
You need to escape the $ with a \
$str =~ s/\$/%26/g
Try escaping the $ with a \:
$str =~ s/\$/%26/g;
Ron

How can I manually interpolate string escapes in a Perl string?

In perl suppose I have a string like 'hello\tworld\n', and what I want is:
'hello world
'
That is, "hello", then a literal tab character, then "world", then a literal newline. Or equivalently, "hello\tworld\n" (note the double quotes).
In other words, is there a function for taking a string with escape sequences and returning an equivalent string with all the escape sequences interpolated? I don't want to interpolate variables or anything else, just escape sequences like \x, where x is a letter.
Sounds like a problem that someone else would have solved already. I've never used the module, but it looks useful:
use String::Escape qw(unbackslash);
my $s = unbackslash('hello\tworld\n');
You can do it with 'eval':
my $string = 'hello\tworld\n';
my $decoded_string = eval "\"$string\"";
Note that there are security issues tied to that approach if you don't have 100% control of the input string.
Edit: If you want to ONLY interpolate \x substitutions (and not the general case of 'anything Perl would interpolate in a quoted string') you could do this:
my $string = 'hello\tworld\n';
$string =~ s#([^\\A-Za-z_0-9])#\\$1#gs;
my $decoded_string = eval "\"$string\"";
That does almost the same thing as quotemeta - but exempts '\' characters from being escaped.
Edit2: This still isn't 100% safe because if the last character is a '\' - it will 'leak' past the end of the string though...
Personally, if I wanted to be 100% safe I would make a hash with the subs I specifically wanted and use a regex substitution instead of an eval:
my %sub_strings = (
'\n' => "\n",
'\t' => "\t",
'\r' => "\r",
);
$string =~ s/(\\n|\\t|\\n)/$sub_strings{$1}/gs;

What's the difference between single and double quotes in Perl?

In Perl, what is the difference between ' and " ?
For example, I have 2 variables like below:
$var1 = '\(';
$var2 = "\(";
$res1 = ($matchStr =~ m/$var1/);
$res2 = ($matchStr =~ m/$var2/);
The $res2 statement complains that Unmatched ( before HERE mark in regex m.
Double quotes use variable expansion. Single quotes don't
In a double quoted string you need to escape certain characters to stop them being interpreted differently. In a single quoted string you don't (except for a backslash if it is the final character in the string)
my $var1 = 'Hello';
my $var2 = "$var1";
my $var3 = '$var1';
print $var2;
print "\n";
print $var3;
print "\n";
This will output
Hello
$var1
Perl Monks has a pretty good explanation of this here
' will not resolve variables and escapes
" will resolve variables, and escape characters.
If you want to store your \ character in the string in $var2, use "\\("
Double quotation marks interpret, and single quotation do not
If you are going to create regex strings you should really be using the qr// quote-like operator:
my $matchStr = "(";
my $var1 = qr/\(/;
my $res1 = ($matchStr =~ m/$var1/);
It creates a compiled regex that is much faster than just using a variable containing string. It also will return a string if not used in a regex context, so you can say things like
print "$var1\n"; #prints (?-xism:\()
Perl takes the single-quoted strings 'as is' and interpolates the double-quoted strings. Interpolate means, that it substitutes variables with variable values, and also understands escaped characters. So, your "\(" is interpreted as '(', and your regexp becomes m/(/, this is why Perl complains.
"" Supports variable interpolation and escaping. so inside "\(" \ escapes (
Where as ' ' does not support either. So '\(' is literally \(