Code does not remove non-ascii characters from variable - perl

Why do the following lines of code not remove non-ascii characters from my variable and replace it with a single space?
$text =~ s/[[:^ascii:]]+/ /rg;
$text =~ s/\h+/ /g;
Whereas this works to remove newline?
$log_mess =~ s/[\r\n]+//g;

To explain the problem for anyone finding this question in the future:
$text =~ s/[[:^ascii:]]+/ /rg;
The problem is the /r option on the substitution operator (s/.../.../).
This operator is documented in the "Regexp Quote-Like Operators" section of perlop. It says this about /r:
r - Return substitution and leave the original string untouched.
You see, in most cases, the substitution operator works on the string that it is given (e.g. your variable $text) but in some cases, you don't want that. In some cases, you want the original variable to remain unchanged and the altered string to be returned so that you can store it in a new variable.
Previously, you would do this:
my $new_var = $var;
$new_var =~ s/regex/substitution/;
But since the /r option was added, you can simplify that to:
my $new_var = $var =~ s/regex/substitution/r;
I'm not sure why you used /r in your code (I guess you copied it from somewhere else), but you don't need it here and it's what is leading to your original string being unchanged.

Related

Converting byte array to decimal

Was wondering if someone could explain this snippet to me in depth
(my $bytearray_val_ascii = $in) =~ s/([a-fA-F0-9]{2})/chr(hex $1)/eg;
The s/// is a regex substitution operator which the =~ operator binds to a variable on its left-hand side, so a statement
$var =~ s/pattern/replacement/;
matches the pattern in the variable $var and performs a substitution of it by the replacement string, evaluated in a double-quoted context. The operation can be tuned and tweaked by modifiers that follow the closing delimiter (and which can also be embedded in the pattern).†
This changes the variable "in-place" -- after this statement $var is changed. An idiom to preserve $var and store the changed string in another variable is to assign $var to that other variable and "then" change it (by ordering operations by parenthesis), all in one statement. And the commonly used idiom is to also introduce a new variable in that statement
(my $new_var = $original) =~ s/.../.../;
Now $original is unchanged, while the changed string is in $new_var (if the pattern matched).
This idiom is nowadays unneeded since a r (non-destructive) modifier was introduced in 5.14
my $new_var = $original =~ s/.../.../r;
The $original is unchanged and the changed string returned, then assigned to $new_var.
The regex itself matches and captures two consecutive alphanumeric characters, runs hex on them and then chr on what hex returns, and uses that result to replace them. It keeps going through the string to do that with all such pairs that it finds.
If this is indeed precisely all that there is to do then it is more simply done using pack
my $bytearray_val_ascii = pack( "H*", $in );
†
Here the modifiers are: e, which makes is so that the replacement side is evaluated as code so that what that code evaluates to is used for replacement; and g which makes it continue searching and replacing through the whole string (not just the first occurrence of pattern that is found).

Percentage sign getting removed when URI decoding

I'm trying to decode a uriencoded bit of form data in Perl (the encoded data is %25admin which should decode to %admin). I'm using some long repeated, simple regex to do it:
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s///g;
This set of regex has served me well for years and usually does just fine, but in this case, it is outputting min ("%ad" is missing from the decoded string as though it were part of the escaped character). What am I missing that it is causing it to interpret the characters %25ad as a single escaped character rather than %25 as the escaped character and ad as independent of it?
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
This successfully converts %25admin to %admin which is actually the result you want. But for some unknown reason you then do another substitute with an empty pattern:
$value =~ s///g;
This empty pattern has a special meaning. From perldoc perlop:
The empty pattern //
If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead.
The last successfully matched regular expression is in the line above, so this statement essentially means:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])//g;
This matches %admin and results in min.

How can I use "s" as a substitution delimiter in Perl?

I was playing with Perl and thought that
sssssss
Would have been the same as
s/s/ss/
It seems only certain delimiters can be used. What are they?
You can use any non-whitespace character as the delimiter, but you can't use the delimiter inside PATTERN or REPLACEMENT without escaping it. This is totally valid:
my $x = 's';
$x =~ s s\ss\s\ss;
print $x; # prints "ss"
Note that a space is required after the first s or else it will be interpreted as ss identifier.

What is the meaning of the number sign (#) in a Perl regex match?

What is the meaning of below statement in perl?
($script = $0) =~ s#^.*/##g;
I am trying to understand the operator =~ along with the statement on the right side s#^.*/##g.
Thanks
=~ applies the thing on the right (a pattern match or search and replace) to the thing on the left. There's lots of documentation about =~ out there, so I'm just going to point you at a pretty good one.
There's a couple of idioms going on there which are not obvious nor well documented which might be tripping you up. Let's cover them.
First is this...
($copy = $original) =~ s/foo/bar/;
This is a way of copying a variable and performing a search and replace on it in a single step. It is equivalent to:
$copy = $original;
$copy =~ s/foo/bar/;
The =~ operates on whatever is on the left after the left hand code has been run. ($copy = $original) evaluates to $copy so the =~ acts on the copy.
s#^.*/##g is the same as s/^.*\///g but using alternative delimiters to avoid Leaning Toothpick Syndrome. You can use just about anything as a regex delimiter. # is common, though I think its ugly and hard to read. I prefer {} because they balance. s{^.*/}{}g is equivalent code.
Unrolling the idioms, you have this:
$script = $0;
$script =~ s{^.*/}{}g;
$0 is the name of the script. So this is code to copy the name of the script and strip everything up to the last slash (.* is greedy and will match as much as possible) off it. It is getting just the filename of the script.
The /g indicates to perform the match on the string as many times as possible. Since this can only ever match once (the ^ anchors it to the beginning of the string) it serves no purpose.
There's a better and safer way to do this.
use File::Basename;
$script = basename($0);
It's very, very simple:
Perl quote-like expressions can take many different characters as part separators. The separator right after the command (in this case, the s) is the separator for the rest of the operation. For example:
# Out with the "Old" and "In" with the new
$string =~ s/old/new/;
$string =~ s#old#new#;
$string =~ s(old)(new);
$string =~ s#old#new#;
All four of those expressions are the same thing. They replace the string old with new in my $string. Whatever comes after the s is the separator. Note that parentheses, curly braces, and square brackets use parings. This works out rather nicely for the q and qq which can be used instead of single quotes and double quotes:
print "The value of \$foo is \"foo\"\n"; # A bit hard to read
print qq/The value of \$foo is "$foo"\n/; # Maybe slashes weren't a great choice...
print qq(The value of \$foo is "$foo"\n); # Very nice and clean!
print qq(The value of \$foo is (believe it or not) "$foo"\n); #Still works!
The last still works because the quote like operators count opening and closing parentheses. Of course, with regular expressions, parentheses and square brackets are part of the regular expression syntax, so you won't see them so much in substitutions.
Most of the time, it is highly recommended that you stick with the s/.../.../ form just for readability. It's what people are use to and it's easy to digest. However, what if you have this?
$bin_dir =~ s/\/home\/([^\/]+)\/bin/\/Users\/$1\bin/;
Those backslashes can make it hard to read, so the tradition has been to replace the backslash separators to avoid the hills and valleys effect.
$bin_dir =~ s#/home/([^/]+)/bin#/Users/$1/bin#;
This is a bit hard to read, but at least I don't have to quote each forward slash and backslash, so it's easier to see what I'm substituting. Regular expressions are hard because good quote characters are hard to find. Various special symbols such as the ^, *, |, and + are magical regular expression characters, and could probably be in a regular expression, the # is a common one to use. It's not common in strings, and it doesn't have any special meaning in a regular expression, so it won't be used.
Getting back to your original question:
($script = $0) =~ s#^.*/##g;
is the equivalent of:
($script = $0) =~ s/^.*\///g;
But because the original programmer didn't want to backquote that slash, they changed the separator character.
As for the:
($script = $0) =~ s#^.*/##g;`
It's the same as saying:
$script = $0;
$script =~ s#^.*/##g;
You're assigning the $script variable and doing the substitution in a single step. It's very common in Perl, but it is a bit hard to understand at first.
By the way, if I understand that basic expression (Removing all characters to the last forward slash. This would have been way cleaner:
use File::Basename;
...
$script = basename($0);
Much easier to read and understand -- even for an old Perl hand.
In perl, you can use many kinds of characters as quoting characters (string, regular expression, list). lets break it down:
Assign the $script variable the contents of $0 (the string that contains the name of the calling script.)
The =~ character is the binding operator. It invokes a regular expression match or a regex search and replace. In this case, it matches against the new variable, $script.
the s character indicates a search and replace regex.
The # character is being used as the delimiter for the regex. The regex pattern quote character is usually the / character, but you can use others, including # in this case.
The regex, ^.*/. It means, "at the start of string, search for zero or more characters until a slash. This will keep capturing on each line except for newline characters (which . does not match by default.)
The # indicating the start of the 'replace' value. Usually you have a pattern here that uses any captured part of the first line.
The # again. This ends the replace pattern. Since there was nothing between the start and end of the replace pattern, everything that was found in the first is replaced with nothing.
g, or global match. The search and replace will keep happening as many times as it matches in the value.
Effectively, searches for and empties every value before the / in the value , but keeps all the newlines, in the name of the script. It's a really lazy way of getting the script name when invoked in a long script that only works with a unix-like path.
If you have a chance, consider replacing with File::Basename, a core module in Perl:
use File::Basename;
# later ...
my $script = fileparse($0);

Simple Perl string Problem

I know this might be very easy to some,,
I have a simple string like this #¨0­+639172523299 (with characters before a mobile number). My question is, how do i remove all the characters before the plus(+)? What i know is to remove a known character as follows:
$number =~ tr/://d; (if i want to remove a colon)
But here, I want all characters before '+' to be removed.
To remove everything up to and including the first +, you can do:
$number ~= s/.*\+//;
If you want to keep the +, you can put that into the replacement:
$number ~= s/.*\+/+/;
The above says: Match "anything" (the .*) followed by a + (+ is a special character in regular expressions, which is why it needs the backslash escape) and replace it with nothing (or in the above example, replace it with a single +).
Note that the above will strip out everything up to the LAST + in the string, which may not be what you want. If you want to keep strip out everything up to the FIRST + in a string, you can do:
$number =~ s/[^+]*\+//;
or
$number =~ s/[^+]*\+/+/; # Keep the +
The difference from the first regular expression being the [^+]* instead of .*, which means "match any character except a +".
For more information on Perl's regular expressions, the perldoc perlre manual page is pretty good, as is O'Reilly's Mastering Regular Expressions book.
in the simplest case
$string =~ s/^.*\+//;
if you have more than one "+" before the mobile number
$string="#+0+0­+639172523299";
#s=split /\+/,$string;
print $s[-1];
In fact, you can just use split() instead of regex. Its easier.
my $string = '#¨0­+639172523299';
$string =~ s/(.*)(?=\+)//;
print $string;
$number =~ s/^.*\+//;
s/(.*?\+)(.*)/\2/;
If you want plus to be remain
s/(.*?)(\+)(.*)/\2\3/;
my $str="#¨0­+639172523299";
if($str=~/(\D+)(\+[0-9]+)/)
{
print $2;
}