Re: Choosing grouping of split items

From: John W. Krahn (krahnj_at_acm.org)
Date: 02/16/04


Date: Mon, 16 Feb 2004 11:32:29 GMT

fishfry wrote:
>
> If I have
>
> $text = 'abc - def - ghi - jkl";
>
> then
>
> @tokens = split(/-/, $text, 3);
>
> splits the line into abc, def, ghi - jkl (ignoring leading and trailing
> spaces).
>
> I have an application where there may be 3 or 4 items separated by '-'
> but if there are 4, I want them returned as abc - def, ghi, and jkl.
>
> Is there a one-liner for that?

$ perl -le'
$text = "abc - def - ghi - jkl";
@tokens = $text =~ /(.+)-(.+)-(.+)/;
print for @tokens;
'
abc - def
 ghi
  jkl

John

-- 
use Perl;
program
fulfillment


Relevant Pages

  • Re: escape sequence for tab not working
    ... This sets $this_str to "abc", followed by a literal tab, followed by ... So echo doesn't get to see ... Here Perl prints the value without shell intervention. ...
    (comp.lang.perl.misc)
  • Re: Searching a string
    ... $ perl -e' ... my $string = <<STRING; ...
    (perl.beginners)
  • RE: perl question - unused variables
    ... declare a variable called '$nothing' in a script on working on now, ... Subject: RE: perl question - unused variables ... $abc; and using strict and -w, I do not get any warning. ...
    (perl.beginners)
  • Re: Re: Removing blank lines from a file
    ... >> I am new to perl and getting confused with pattern matching. ...
    (perl.beginners)
  • Re: regular expression and not form
    ... How can I express "match any string except 'ABC'" in regular ... If you can use tools supporting Perl-compatible regex, like Perl, ...
    (comp.unix.shell)