$str = " some text ";
$str =~ s/^\s+//;
$str =~ s/\s+$//;
You can do it with one regexp but it might be a little slower, only testing would tell for sure:
Code:
$str = " some text ";
$str =~ s/^\s+|\s+$//g;
As far as a simpler way, I don't think so.
---------- Post updated at 11:45 AM ---------- Previous update was at 11:30 AM ----------
Run this on the machine that will run the code to see which is best for that machine:
Code:
#!/usr/bin/perl
use warnings;
use strict;
use Benchmark qw(cmpthese timethese);
sub double_star {
my $string = shift;
$string =~ s/^\s*//;
$string =~ s/\s*$//;
return $string;
}
sub double_plus {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub single_or {
my $string = shift;
$string =~ s/^\s+|\s+$//g;
return $string;
}
sub replace {
my $string = shift;
$string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
return $string;
}
sub for_star {
my $string = shift;
for ($string) { s/^\s+//; s/\s+$//; }
return $string;
}
sub for_plus {
my $string = shift;
for ($string) { s/^\s*//; s/\s*$//; }
return $string;
}
sub regex_or {
my $string = shift;
$string =~ s/(?:^ +)||(?: +$)//g;
return $string;
}
cmpthese(
-1,
{
'single_or' => q|single_or( ' Mary had a little lamb. ');|,
'double_star' => q|double_star(' Mary had a little lamb. ');|,
'double_plus' => q|double_plus(' Mary had a little lamb. ');|,
'replace' => q|replace( ' Mary had a little lamb. ');|,
'for_star' => q|for_star( ' Mary had a little lamb. ');|,
'for_plus' => q|for_plus( ' Mary had a little lamb. ');|,
'regex_or' => q|regex_or( ' Mary had a little lamb. ');|,
}
);
Results might vary machine to machine and perl version to perl version.
Last edited by KevinADC; 08-26-2009 at 01:37 PM..
Reason: added "g" to the last regexp
In a shell script, how I can achieve substitution of shell script var to a regular
expression, as shown below.
var=`head -1 file1`
awk '$0!~/$var/ {print $0}' file1 > file2
In the case above $var value literally considered for non-exists criteria. (3 Replies)
Hello!
I've got txt-file containing lots of data in sentences like this:
;;BA;00:00:03:00;COM;CLOQUET-LAFOLLYE;SIMON;
but sometime more than on in a line like this:
;;BA;00:00:03:00;COM;CLOQUET-LAFOLLYE;SIMON;;;BA;00:00:03:00;REA;RTL9;;;;BAC;:00;TIT;SEMAINE SPECIALE ~SSLOGAN~T DVD;;
... (3 Replies)
Hi there!
I'm trying to validate a simple 6 digits number with reg expr. I ONLY want 6 digits so when i type 7 digits the script should no validate the number.
I've write this code:
#!/usr/bin/perl
while(<STDIN>){
if($_=~/\d{6}/){
print "Bingo!\n";
... (2 Replies)
Hi all,
I want to cut a column which match the regular expression "beta", if I don't know the column number?
cat test
alpha;beta;gamma
11;22;33
44;55;66
77;88;99
should be
command ....
beta
22
55 (6 Replies)
Hi All,
I need your input on how to mask out / ignore a string that does not match a working regular expression (continually refining) pattern in Java. Below is the code snippet which is picking up all the lines with the correct regular expression string except one known so far:
public... (0 Replies)
I am trying to check if files staring with filename but ending with diffent dates e.g. filename.2011-10-25.
The code I am using is below
if
It works find only if one file is present but returns binary operator expected
when there are mulptiple files.
Please help me correcting it. I... (5 Replies)
Hi all,
I need to do scrip for printing starting and ending numbers along with count in given file.:wall:
Input: a.txt
10000030
10000029
10000028
10000027
10000026
10000024
10000023
10000021
10000018
10000018
10000017
10000016
10000015
10000014 (2 Replies)
Hello,
Here is my text data excerpted from the webpage:
input
My target is to get:
What i tried is:
sed 's/.*\(connector\)/1/' input > output
but all characters coming before the word "connector" are deleted which is not good for me.
My question: (9 Replies)
im trying to search for a WORD in a file which
begins with a number
followed by a hypen
follwed multiple words
and end with a dot "."
and pront the entire line which matches the above.
Please note that there is a space at the begining of each line
i/p file
19458 00000-CONTROL-PARA.... (5 Replies)
Perl::Critic::Policy::InputOutput::RequireBracedFileHandUsertContributed PerPerl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint(3)NAME
Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint - Write "print {$FH} $foo, $bar;" instead of "print $FH $foo, $bar;".
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
The "print" and "printf" functions have a unique syntax that supports an optional file handle argument. Conway suggests wrapping this
argument in braces to make it visually stand out from the other arguments. When you put braces around any of the special package-level
file handles like "STDOUT", "STDERR", and "DATA", you must the '*' sigil or else it won't compile under "use strict 'subs'".
print $FH "Mary had a little lamb
"; #not ok
print {$FH} "Mary had a little lamb
"; #ok
print STDERR $foo, $bar, $baz; #not ok
print {STDERR} $foo, $bar, $baz; #won't compile under 'strict'
print {*STDERR} $foo, $bar, $baz; #perfect!
CONFIGURATION
This Policy is not configurable except for the standard options.
AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
perl v5.16.32014-06-Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint(3)