×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Trim whitespaces > beginning and end

Trim whitespaces > beginning and end

Trim whitespaces > beginning and end

(OP)

Hi

I'm trying to get rid of the whitespaces at the beginnning and at the end of a string.
Chomp does trim the trailing spaces, but that's it.

So the following string "   this is a string    " would become "this is a string"

RE: Trim whitespaces > beginning and end

(OP)

I'm using these regex's right now:
Can they be combined or simplified ?

CODE

$Chars =~ s/^\s+//;
$Chars =~ s/\s+$//;

RE: Trim whitespaces > beginning and end

CODE

s/^\s*(.*\S)\s*$/$1/;

Trojan.

RE: Trim whitespaces > beginning and end

CODE

s/^\s*(.*?)\s*$/$1/;

also empties strings only containing white space (just in case this makes any difference).

Holger

RE: Trim whitespaces > beginning and end

the way you are doing it looks to be the best, it will most likely be faster than the other suggestions. Sometimes two regexps are better than one!

RE: Trim whitespaces > beginning and end

I'm not sure I agree with you Kevin.
With two regexs your have two compilations and that probably takes much longer than executing the regex.
If anyone fancies testing them I'd be interested to see which is actually faster.

Trojan.

RE: Trim whitespaces > beginning and end

Looks as if Kevin is totally right:

CODE

#!/usr/bin/perl -w
use strict;

my @strings = ("  foo   ", "    bar    ", "   foo bar   ");

for (1..100000) {
  foreach my $string (@strings) {
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
  }
}

results in

real    0m1.021s
user    0m1.000s
sys     0m0.000s

whereas Trojans solution results in

real    0m4.067s
user    0m4.030s
sys     0m0.000s

and finally mine in

real    0m4.653s
user    0m4.610s
sys     0m0.000s

RE: Trim whitespaces > beginning and end

Full marks to Kevin!

Trojan.

RE: Trim whitespaces > beginning and end

O'Reilly's Mastering Regular Expressions recommends the two regex solution because, as pointed out, its faster.

RE: Trim whitespaces > beginning and end

Yep, we noticed that!
I think it's also more readable.
A star for Kevin for being smarter than the rest of us!
(you know you can go off people!)  

Trojan.

RE: Trim whitespaces > beginning and end

(OP)
Thanks to all for the help.

regex7
Thank you for your input and for the stats.

Kevin & Trojan
As usual, thank you very much for your help. It's always appreciated to get your advice.

RE: Trim whitespaces > beginning and end

my hat size just got 2 sizes bigger!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close