Sponsored Content
Top Forums Shell Programming and Scripting perl: reg.expr: combine starting and ending removal in one exprecion Post 302347808 by KevinADC on Wednesday 26th of August 2009 12:45:58 PM
Old 08-26-2009
this is probably the most efficient way:

Code:
$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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text replace by position instead of reg expr.

Can we replace the contents the of the rows of file, from one position to another position by mentioning, some start position & the width? (4 Replies)
Discussion started by: videsh77
4 Replies

2. Shell Programming and Scripting

var substitution in a reg expr ?

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)
Discussion started by: videsh77
3 Replies

3. UNIX for Dummies Questions & Answers

scipt dividing strings /reg expr

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)
Discussion started by: maco_home
3 Replies

4. Shell Programming and Scripting

PERL: Simple reg expr validate 6 digits number

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)
Discussion started by: BufferExploder
2 Replies

5. Shell Programming and Scripting

print column that match reg expr

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)
Discussion started by: research3
6 Replies

6. Programming

How to prevent incorrect string using reg expr in Java?

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)
Discussion started by: gjackson123
0 Replies

7. Shell Programming and Scripting

if statement to check files with different ending but same starting name

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)
Discussion started by: ningy
5 Replies

8. UNIX for Advanced & Expert Users

Pring starting and ending numbers using UNIX

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)
Discussion started by: jackbell2013
2 Replies

9. Shell Programming and Scripting

Text to column starting/ending with special character in each row

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)
Discussion started by: baris35
9 Replies

10. UNIX for Beginners Questions & Answers

Search for words starting and ending with

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)
Discussion started by: anijan
5 Replies
Mail::DKIM::TextWrap(3pm)				User Contributed Perl Documentation				 Mail::DKIM::TextWrap(3pm)

NAME
Mail::DKIM::TextWrap - text wrapping module written for use with DKIM SYNOPSIS (FOR MAIL::DKIM USERS) use Mail::DKIM::TextWrap; Just add the above line to any program that uses Mail::DKIM::Signer and your signatures will automatically be wrapped to 72 characters. SYNOPSIS (FOR OTHER USERS) my $output = ""; my $tw = Mail::DKIM::TextWrap->new( Margin => 10, Output => $output, ); $tw->add("Mary had a little lamb, whose fleece was white as snow. "); $tw->finish; print $output; DESCRIPTION
This is a general-purpose text-wrapping module that I wrote because I had some specific needs with Mail::DKIM that none of the contemporary text-wrapping modules offered. Specifically, it offers the ability to change wrapping options in the middle of a paragraph. For instance, with a DKIM signature: DKIM-Signature: a=rsa; c=simple; h=first:second:third:fourth; b=Xr2mo2wmb1LZBwmEJElIPezal7wQQkRQ8WZtxpofkNmXTjXf8y2f0 the line-breaks can be inserted next to any of the colons of the h= tag, or any character of the b= tag. The way I implemented this was to serialize the signature one element at a time, changing the text-wrapping options at the start and end of each tag. TEXT WRAPPING OPTIONS
Text wrapping options can be specified when calling new(), or by simply changing the property as needed. For example, to change the number of characters allowed per line: $tw->{Margin} = 20; Break a regular expression matching characters where a line break can be inserted. Line breaks are inserted AFTER a matching substring. The default is "/s/". BreakBefore a regular expression matching characters where a line break can be inserted. Line breaks are inserted BEFORE a matching substring. Usually, you want to use Break, rather than BreakBefore. The default is "undef". Margin specifies how many characters to allow per line. The default is 72. If no place to line-break is found on a line, the line will extend beyond this margin. Separator the text to insert when a linebreak is needed. The default is " ". If you want to set a following-line indent (e.g. all lines but the first begin with four spaces), use something like " ". Swallow a regular expression matching characters that can be omitted when a line break occurs. For example, if you insert a line break between two words, then you are replacing a "space" with the line break, so you are omitting the space. On the other hand, if you insert a line break between two parts of a hyphenated word, then you are breaking at the hyphen, but you still want to display the hyphen. The default is "/s/". CONSTRUCTOR
new() - create a new text-wrapping object my $tw = Mail::DKIM::TextWrap->new( Output => $output, %wrapping_options, ); The text-wrapping object encapsulates the current options and the current state of the text stream. In addition to specifying text wrapping options as described in the section above, the following options are recognized: Output a scalar reference, or a glob reference, to specify where the "wrapped" text gets output to. If not specified, the default of STDOUT is used. METHODS
add() - process some text that can be wrapped $tw->add("Mary had a little lamb. "); You can add() all the text at once, or add() the text in parts by calling add() multiple times. finish() - call when no more text is to be added $tw->finish; Call this when finished adding text, so that any remaining text in TextWrap's buffers will be output. flush() - output the current partial word, if any $tw->flush; Call this whenever changing TextWrap's parameters in the middle of a string of words. It explicitly allows a line-break at the current position in the string, regardless of whether it matches the current break pattern. perl v5.10.1 2010-02-28 Mail::DKIM::TextWrap(3pm)
All times are GMT -4. The time now is 01:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy