How to match \n at the end of variable using regexp - perl

I stored a file in an variable, say $buffer. There is a "\n" at the end of $buffer. I want to replace it with a empty value. I tried
regexp "\n$" $buffer
Not working. The code is in TCL, but I need to know how we can do it in either Perl or TCL.

string trim $buffer \n
See the manual.

In Perl chomp removes the end-of-record separator. So, to remove the '\n' all you need in is chomp $buffer.

How about: regsub {\n$} $buffer ""

chomp is probably best, as #Borodin said, but you can also use \z to match only at the end of the string:
$buffer =~ s/\n\z//;

In Perl:
$buffer =~ s/\n$//;
=~ is the binding operator, s is the substitution operator, the / are delimiters for the operands, so \n$ is replaced by the empty string.
chomp($buffer) will accomplish the same thing.

Check whether this works.
regsub "\n$" "" $buffer
For further reading

Related

Code does not remove non-ascii characters from variable

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.

Difference between chomp and trim in Perl?

What is the difference between chomp and trim in Perl? Which one is better to use and when?
Chomp: The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.
For more information see chomp.
As rightfold has commented There is no trim function in Perl. Generally people write a function with name trim (you can use any other name also) to remove leading and trailing white spaces (or single or double quotes or any other special character)
trim remove white space from both ends of a string:
$str =~ s/^\s+|\s+$//g;
trim removes both leading and trailing whitespaces, chomp removes only trailing input record separator (usually new line character).
Chomp: It only removes the last character, if it is a newline.
More details can be found at: http://perldoc.perl.org/functions/chomp.html
Trim: There is no function called Trim in Perl. Although, we can create our function to remove the leading and trailing spaces in Perl. Code can be as follows:
perl trim function - remove leading and trailing whitespace
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
More details can be found at :http://perlmaven.com/trim

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.

Perl output overwrites itself

I have code which loops through lines in a file, and tries to print out each line with something added at the start and end.
However, I get output like this: "nominalte cows".
Basically, the bit after the line (nominal) overwrites the start of it. I know that removing the chomp and regex lines stops this effect, but I need it to be on one line without spaces. Where am I going wrong?
while ($line = <INPUT>) {
chomp $line;
$line =~ s/ //g;
printf "\#attribute %s nominal\n", $line;
}
Your input file is probably from MS Windows with end of line encoded as CR-LF. You can also just s/\r// to remove the CR.
You might have \r in your variable. Try using \s:
$line =~ s/\s//g;
See perlre for the meaning of \s.

In perl pattern matching..how to exclude the \n character from pattern

I am new to perl and writing my first few programs and using its pattern matching abilities. I am reading a file into array like this:
#list=<file>
Then indexing each line of array by $list[0..9] etc, and when I match it against a pattern, the $list[0] includes \n character, hence the match fails. So if ($string =~ $list[0]) fails though without \n character in pattern it would match.
How do I tell pattern matcher to not consider the \n character from pattern?
Thanks
You can shave the line ends from the array after reading:
#lines = …;
chomp #lines;
Now #lines contains the lines without line ends. See perldoc chomp for details.
If you want to remove the \n from your lines you can:
chomp $list[0]
see perldoc -f chomp for the details.
This is a good opportunity to get to know how Perl modules work.
You can for example use Perl6::Slurp which will both a) parse the file b) put the contents in an array c) remove the newline characters for you.
For example:
use Perl6::Slurp;
my #lines = slurp '<:utf8', 'filename', {chomp=>"\n"}
This will match with the \n:
if ( $list[0] =~ "$string\n")
Or if you want the \n to be optional:
if ( $list[0] =~ /$string\n?/ )