Regular Expression

Discussion in 'Perl' started by Ray, Aug 31, 2003.

  1. Ray

    Ray Guest

    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?
    Ray, Aug 31, 2003
    #1
    1. Advertising

  2. "Ray" <> wrote in message
    news:SRc4b.1290$...
    > 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
    George Karabotsos, Aug 31, 2003
    #2
    1. Advertising

  3. Ray

    Ray Guest

    George Karabotsos wrote:

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


    Thanks, it's working now.
    Ray, Aug 31, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    441
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,272
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    832
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,924
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page