$str = q{split on whitespace "but leave" quoted "spaces alone"}; @chunks = get_chunks($str); # desired return value: # ('split', 'on', 'whitespace', 'but leave', 'quoted', 'spaces alone') # it's hard to do it with split(), but it CAN be done! # method 1 # note: this leaves the "s in... sub get_chunks { my$s;split/"(?{$s=!$s})(?!)|\s+(?(?{$s})(?!))/,pop } # method 2 # note: this removes the "s (preferred) sub get_chunks { my$s;@_=split/(?(?{$s})"\s*(?{$s=0})|(?:\s*"(?{$s=1})|\s+))/,' '.pop;s +hift;@_ }
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Two Eclectic split()s
by jryan (Vicar) on May 21, 2003 at 07:57 UTC | |
|
Re: Two Eclectic split()s (another one maybe?)
by Enlil (Parson) on May 21, 2003 at 09:15 UTC | |
by japhy (Canon) on May 21, 2003 at 14:18 UTC | |
|
Re: Two Eclectic split()s
by Aristotle (Chancellor) on May 21, 2003 at 15:40 UTC | |
by japhy (Canon) on May 21, 2003 at 15:48 UTC | |
|
5.9.0?
by toma (Vicar) on May 26, 2003 at 23:02 UTC | |
by Abigail-II (Bishop) on May 26, 2003 at 23:19 UTC |