in reply to String manipulation
Something like this may do the job:
use strict; use warnings; use feature qw/ :5.10 /; my @str = ("j k l foobar", "foobar", "jkl foobar", "1 2 3", + "jk l foobar", "foobar j k l", "foobar j kl", " ", " ", "j + jk foobar", "j k jk foobar", "j k l"); my $rx = qr/^((?:\w(?:\s+|\z))*)(.*)/; for my $str (@str){ my ($i,$s) = $str =~ /$rx/; $i =~ s/\s+//g; say $i ? join " ",$i,$s : $s; }
Outputs:
jkl foobar foobar jkl foobar 123 jk l foobar foobar j k l foobar j kl j jk foobar jk jk foobar jkl
citromatik
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: String manipulation
by abhy (Novice) on Jun 24, 2009 at 10:42 UTC | |
by citromatik (Curate) on Jun 24, 2009 at 11:22 UTC | |
by Limbic~Region (Chancellor) on Jun 24, 2009 at 13:03 UTC |
In Section
Seekers of Perl Wisdom