On May 22, Peter Cline said:

>Try this.
>my $text = "Browser/Version Platform";
>my ($keep,$discard) = split / /, $text;
>print "$keep\n";
>
>This splits on space and saves the part you want to the variable $keep and 
>the rest to $discard.

Actually, it splits on EXACTLY one space.  That can cause problems:

  @parts = split / /, "this  and that";
  { local $" = ") (";  print "(@parts)"; }
  # (this) () (and) (that)

You might want to use the special split ' ' syntax:

  @parts = split ' ', "this  and that";
  { local $" = ") (";  print "(@parts)"; }
  # (this) (and) (that)

>my $text = "Browser/Version Platform";
>$text =~ /(.+)\s.+/;
>print "$1\n";

That reads too much into $1.  The OP asked for the FIRST whitespace, and
you've given him the LAST whitespace.  The second .+ is also not needed,
and can break certain cases:

  "foobar "      =~ /(.+)\s.+/;  # can't match!
  "foo bar blat" =~ /(.+)\s.+/;  # matches "foo bar"

Here's the output from 'explain':

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

So you see that the .+ matches AS MUCH AS IT CAN.

How about:

  ($wanted) = $string =~ /(\S*)/;

or

  ($wanted) = split ' ', $string;

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **

Reply via email to