Regular Expression

R

Ray

I'm working on a Perl script that reads a text file. The file could have
multiple new lines together, could have lots of spaces between words, tabs,
and so on. Bottome line, I want to end up with the text from the file all
in a single line, with nothing but single spaces between words.

So my Perl script reads the file into an array and puts the lines together
with join. That much is good. From what modest amount I know about regular
expressions, I'd expect to be able to get the extra spaces out this way:

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

....but I seem to be wrong to expect that.

How can I get what I want?
 
G

George Karabotsos

Ray said:
I'm working on a Perl script that reads a text file. The file could have
multiple new lines together, could have lots of spaces between words, tabs,
and so on. Bottome line, I want to end up with the text from the file all
in a single line, with nothing but single spaces between words.

So my Perl script reads the file into an array and puts the lines together
with join. That much is good. From what modest amount I know about regular
expressions, I'd expect to be able to get the extra spaces out this way:

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

...but I seem to be wrong to expect that.

How can I get what I want?

This should work:
$text =~ s/\s+/ /gm;

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top