CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A small regexp question...

 



yanivr78
Novice

Apr 9, 2013, 1:23 PM

Post #1 of 12 (843 views)
A small regexp question... Can't Post

Hello everyone :),

I am trying to remove any whitespace from my output but I seem to be doing something wrong.

here is what I have :


Code
$e = "\t White Space at the end and beginning \t"; 
$f = "this is a new line";
$g = "$e$f";
$g =~ s/^\s+|\s+$//g; # Strip leading, trailing whitespace


the output is :

Code
White Space at the end and beginning     this is a new line


what do I need to add in order to take care of the whitespace between "beginning" and "this" ?

Many thanks for any answer !


yanivr78
Novice

Apr 9, 2013, 1:53 PM

Post #2 of 12 (835 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

found it !
$g =~ s/^\s+|\t|\s+$//g; # Strip leading, trailing whitespace
Cool

but if you have another interesting way to do it please do share ! :)


Laurent_R
Veteran / Moderator

Apr 9, 2013, 1:56 PM

Post #3 of 12 (834 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

You can use your regex on each of the the individual strings, before concatenating them.

But once the strings are joined, your regex will obviously not find and remove spaces in the middle of the string since is is taylored to look for white spaces only at the beginning and the end of the string.

You can also use a second regex:


Code
$g =~ s/\s+/ /g;


which will replace multiple spaces (including tabs) anywhere in the concatenated string by a single space (a "space bar" space).


Laurent_R
Veteran / Moderator

Apr 9, 2013, 2:03 PM

Post #4 of 12 (828 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post


In Reply To
found it !
$g =~ s/^\s+|\t|\s+$//g; # Strip leading, trailing whitespace
Cool

but if you have another interesting way to do it please do share ! :)


This won't work as you probably expect if you have:


Code
$e = "\tWhite Space at the end and beginning\t";  
$f = "this is a new line";


Because it will remove any space between the two sentence and you will obtain:

"White Space at the end and beginningthis is a new line"

with no space between the two merged sentences.


yanivr78
Novice

Apr 9, 2013, 2:09 PM

Post #5 of 12 (825 views)
Re: [Laurent_R] A small regexp question... [In reply to] Can't Post

Hmmm....Strange,

$g =~ s/^\s+|\t|\s+$//g;
works for me , this is the output :
White Space at the end and beginning this is a new line

btw, I tried to use your examples, the only one that almost worked was :
$g =~ s/\s+/ /g;
which has 1 bug...there's a space in the beginning of the actual output :
White Space at the end and beginning this is a new line


(This post was edited by yanivr78 on Apr 9, 2013, 2:10 PM)


g4143
Novice

Apr 9, 2013, 2:13 PM

Post #6 of 12 (823 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

A rookie could try it twice


Code
$g =~ s/^\s+|\s+$//g; # Strip leading, trailing whitespace 
$g =~ s/\s+/ /g;#replace spaces with space



Kenosis
User

Apr 9, 2013, 2:45 PM

Post #7 of 12 (810 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

Or you could just say remove whitespaces at the beginning or tabs anywhere:


Code
s/(^\s+)|\t//g



(This post was edited by Kenosis on Apr 9, 2013, 2:47 PM)


yanivr78
Novice

Apr 9, 2013, 2:47 PM

Post #8 of 12 (808 views)
Re: [Kenosis] A small regexp question... [In reply to] Can't Post

Why are the parentheses for ?


Kenosis
User

Apr 9, 2013, 2:48 PM

Post #9 of 12 (805 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

Actually, good question. They're not needed.


yanivr78
Novice

Apr 9, 2013, 2:49 PM

Post #10 of 12 (803 views)
Re: [Kenosis] A small regexp question... [In reply to] Can't Post

Thank you guys ! you all have been a great help !
I love to be with you in this forum, never failed me even once !
Thank you so much :)


(This post was edited by yanivr78 on Apr 9, 2013, 2:49 PM)


BillKSmith
Veteran

Apr 9, 2013, 4:07 PM

Post #11 of 12 (795 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post

You really want to remove all but one whitespace character here. Use a positive lookbehind assertion.

Code
$e = "\t White Space at the end and beginning \t"; 
$f = "this is a new line";
$g = "$e$f";
$g =~ s/^\s+|(?<=\s)\s+|\s+$//g; # Strip leading, trailing whitespace
print $g;

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 10, 2013, 4:56 AM

Post #12 of 12 (786 views)
Re: [yanivr78] A small regexp question... [In reply to] Can't Post


In Reply To
Hmmm....Strange,

$g =~ s/^\s+|\t|\s+$//g;
works for me , this is the output :
White Space at the end and beginning this is a new line


I guess you misunderstood my point.

Consider the following session under the debugger:


Code
  DB<4> $e = "\tWhite Space at the end and beginning\t"; 

DB<5> $f = "this is a new line";

DB<6> $g = "$e$f";

DB<7> print $g
White Space at the end and beginning this is a new line
DB<8> $g =~ s/^\s+|\t|\s+$//g;

DB<9> print $g
White Space at the end and beginningthis is a new line


The separation space betwween the two strings is lost. It was OK in your example only because you had tabs plus spaces, it does not work if you have only tabs.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives