Regular Expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray

    Regular Expression

    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?


  • George Karabotsos

    #2
    Re: Regular Expression

    "Ray" <wonderfulray@y ahoo.com> wrote in message
    news:SRc4b.1290 $L6.895@bignews 6.bellsouth.net ...[color=blue]
    > 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,[/color]
    tabs,[color=blue]
    > 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?
    >
    >[/color]

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

    George


    Comment

    • Ray

      #3
      Re: Regular Expression

      George Karabotsos wrote:
      [color=blue]
      > This should work:
      > $text =~ s/\s+/ /gm;[/color]

      Thanks, it's working now.







      Comment

      Working...