By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

how to replace multiple white spaces with a single space using regex?

P: 34
Expand|Select|Wrap|Line Numbers
  1. my $tsr = "i am         a                                     good girl";
  2.  
i want output will be,
"i am a good girl"

Any suggestions??????/
Jan 9 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
i.e my $tsr = "i am a good girl";
i want output will be,
"i am a good girl"

Any suggestions??????/
Next time show some effort before posting a question.

Expand|Select|Wrap|Line Numbers
  1. $tsr =~ s/\s+/ /g;
And use the code tags to post code. See the posting guidelines.
Jan 9 '08 #2

P: 40
You can also use this Regex. to produce exact output.

Expand|Select|Wrap|Line Numbers
  1. $tsr =~ s/ {1,}/ /g;
  2.  
Jan 9 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
You can also use this Regex. to produce exact output.

Expand|Select|Wrap|Line Numbers
  1. $tsr =~ s/ {1,}/ /g;
  2.  
Essentially, that does the same thing as Kevin's regex. It produces the "exact" same output.
Jan 9 '08 #4

P: 40
Yes, it produce exact output.
but, I thought there may be line break in the given string.

Essentially, that does the same thing as Kevin's regex. It produces the "exact" same output.
Jan 9 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
Yes, it produce exact output.
but, I thought there may be line break in the given string.
doesn't look like it. The text is included in the variable assignment.

Regards,

Jeff
Jan 9 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.