Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

parse a string

by Anonymous Monk
on May 03, 2002 at 14:41 UTC ( #163798=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to take a string, find the end of it and work my way back to the first whitespace I encounter. How do I do this? EX: (" something, something, something, this ") I want to get the this out of the string. Thanks.

Replies are listed 'Best First'.
Re: parse a string
by particle (Vicar) on May 03, 2002 at 14:45 UTC
    see rindex and substr

    or split and pop

    if you have problems with code, post, and we'll gladly help.

    ~Particle *accelerates*

Re: parse a string
by ferrency (Deacon) on May 03, 2002 at 14:47 UTC
    One way to do this is to use a regular expression:

    my $string = "something, something, something, this"; my ($lastword) = $string =~ /\s(\S+)$/;
    \s matches a space character. (\S+) matches one or more non-whitespace characters, and captures them. $ matches the end of line. Be sure to pay attention to whether your string has a newline at the end or not, and remove it or match for it as appropriate.

    You could also use split(), but I'll leave that one for someone else.


      Here is the split way for Anony:
      my $string = "something, something, something, this"; my $lastword = ( split(/\s*,\s*/, $string) )[-1];
      First, you split on a comma, but since there is whitespace involved, i chose to use a regex that says "zero or more whitespace followed by a literal comma followed by zero or more whitespace". Since split returns a list, you can take advantage and add an index on the spot without using a temporary variable (well, sort of). In this case the index is -1, which is the last element.

      Of coures, this is not an efficient way to do it, but ... TIMTOWTDI! ;)


      (the triplet paradiddle with high-hat)
Re: parse a string
by Molt (Chaplain) on May 03, 2002 at 14:48 UTC

    You can also do this with a simple regexp matching 'non-whitespace characters followed by end of string'.

    #!/usr/bin/perl -w use strict; my $test = "something something this"; $test =~ /(\S*)$/; print "Got $1\n";
Re: parse a string
by thelenm (Vicar) on May 03, 2002 at 15:18 UTC
    A couple people have suggested a regular expression matching non-whitespace at the end of the string, which is a good solution. You can also take advantage of the greediness of the star operator, like this:
    $text =~ /.*\s/; $index = $+[0]-1; # $index now contains the offset of the last space char
Re: parse a string
by talexb (Canon) on May 03, 2002 at 14:46 UTC
    Something like
    my $String = " something, something, something, this "; my ( $LastWord ) = $String =~ m/(\w+)\s$/;
    would probably work. The '$' anchors the pattern to the end of the string, and the brackets around the LastWord variable are to keep things in a list context.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

      This works provided the last section consists of only alphanumerics, since \w won't match anything else. Try this on the string 'Some people don't' and it only matches the 't', using the \S solution is probably safer in the long term.

      Nice use of the inline substitution though, really should get more used to using that idiom.

Re: parse a string
by thunders (Priest) on May 03, 2002 at 15:03 UTC
    This might be slightly silly way to do it, and I'm not a regex expert. But I think this way your regex doesn't have to work through the whole string.
    #!/usr/bin/perl $string = "something, something, something, this"; $string = reverse $string; $string =~ s/\S+\s//; $string = reverse $string; print $string;

    UPDATE:japhy's answer is more efficient. I've updated my code it was originally $string =~ s/\S+\s(.*)/$1/;
      That's almost how I would do it, since I reverse things like that all the time. Your substitution, though, does too much work. $string =~ s/\S+\s//; is good enough.

      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://163798]
Approved by dsb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2018-06-18 20:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.