On Tue, Feb 10, 2004, Hagay Unterman wrote about "RE: Regexps":
> tr " " "\n"
>...
> 
> (replaces spaces with \n only if its not encapsulated in ")

You didn't read his question properly...

And now for a correct (I think) answer:

One really interesting trick (I think) on how to do what you want is to
notice the following fact: spaces on which we should break have an *even*
number of quotes (0, 2, 4, etc.) after them, while spaces which have an
*odd* number of quotes should not be broken on. This assumes that the quotes
indeed are well-formed (come in pairs).

You can do this in Perl, in a one unbelievably ugly (but working :)) regular
expression:
         s/ (?=([^"]*("[^"]*"[^"]*)*)$)/\n/og

for example:

     $ echo 'hi "there man" hello man "hey aa" aaa' |
       perl -e 'foreach(<>){ print;
                             s/ (?=([^"]*("[^"]*"[^"]*)*)$)/\n/og;
                             print}'

     hi "there man" hello man "hey aa" aaa
     hi
     "there man"
     hello
     man
     "hey aa"
     aaa

This RE uses the ?= lookahead pattern - see perlre(1). This is a Perl-
specific feature that is not available in most RE implementations.


-- 
Nadav Har'El                        |     Tuesday, Feb 10 2004, 18 Shevat 5764
[EMAIL PROTECTED]             |-----------------------------------------
Phone: +972-53-790466, ICQ 13349191 |Hi! I'm a signature virus! Copy me into
http://nadav.harel.org.il           |your signature to help me spread!

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to