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.
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* | [reply] |
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";
| [reply] [d/l] |
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.
Alan | [reply] [d/l] |
|
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! ;)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] |
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
| [reply] [d/l] |
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/; | [reply] [d/l] [select] |
|
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:??;
| [reply] [d/l] |
Re: parse a string
by talexb (Chancellor) on May 03, 2002 at 14:46 UTC
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|