zsh-users
 help / color / mirror / code / Atom feed
* Splitting on unquoted equals
@ 2015-05-06  6:28 Jesper Nygårds
  2015-05-06  7:06 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Nygårds @ 2015-05-06  6:28 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 876 bytes --]

I am trying to extract url:s from an XML document, where I have fragments
resembling the following:

<element name="myelement" url="http://site.com/path/config?myname=user">

Say I assign the above to a variable:

 % mystring="<element name=\"myelement\" url=\"
http://site.com/path/config?myname=user\">"

Is there a way to split this string on '=' but not within the quotes? If I
only do this:

%  print -l ${(s:=:)mystring}
<element name
"myelement" url
"http://site.com/path/config?myname
user">

The url has of course been split on '=' as well, which is not what I want.
Is there a way to say "split on '=' but not within quotes", similar to the
way (z) treats white space?

% myspaces="this is \"three arguments\""

% print -l ${(z)myspaces}
this
is
"three arguments"

Here the space is preserved within quotes. Is there a way to achieve
something similar but with '='?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Splitting on unquoted equals
  2015-05-06  6:28 Splitting on unquoted equals Jesper Nygårds
@ 2015-05-06  7:06 ` Bart Schaefer
  2015-05-06  7:25   ` Jesper Nygårds
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-05-06  7:06 UTC (permalink / raw)
  To: Zsh Users

On May 6,  8:28am, Jesper Nygards wrote:
}
} Is there a way to say "split on '=' but not within quotes", similar to the
} way (z) treats white space?

Not really.  (z) works because it "understands" shell syntax.  You are
asking for the shell to "understand" HTML syntax.  It does not.  You
have two choices:

(1) Write an actual HTML element parser, in shell script.  (Probably
easier to use Perl HTML::Parser or similar.)

(2) Approach the problem differently.  For example, if you first split
on double-quotes and spaces and then strip off trailing equal signs:

torch% print -l ${${=${(s'"')mystring}}%=}
<element
name
myelement
url
http://site.com/path/config?myname=user
>

Curious aside - this doesn't work:

torch% print -l ${(s:\":)string}    
<element name="myelement" url="http://site.com/path/config?myname=user">

But you can't use an un-escaped quote mark there either:

torch% print -l ${(s:":)string}       
braceparam dquote> 

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Splitting on unquoted equals
  2015-05-06  7:06 ` Bart Schaefer
@ 2015-05-06  7:25   ` Jesper Nygårds
  0 siblings, 0 replies; 3+ messages in thread
From: Jesper Nygårds @ 2015-05-06  7:25 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1175 bytes --]

Thank you. Your alternative approach works brilliantly for my problem.


On Wed, May 6, 2015 at 9:06 AM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On May 6,  8:28am, Jesper Nygards wrote:
> }
> } Is there a way to say "split on '=' but not within quotes", similar to
> the
> } way (z) treats white space?
>
> Not really.  (z) works because it "understands" shell syntax.  You are
> asking for the shell to "understand" HTML syntax.  It does not.  You
> have two choices:
>
> (1) Write an actual HTML element parser, in shell script.  (Probably
> easier to use Perl HTML::Parser or similar.)
>
> (2) Approach the problem differently.  For example, if you first split
> on double-quotes and spaces and then strip off trailing equal signs:
>
> torch% print -l ${${=${(s'"')mystring}}%=}
> <element
> name
> myelement
> url
> http://site.com/path/config?myname=user
> >
>
> Curious aside - this doesn't work:
>
> torch% print -l ${(s:\":)string}
> <element name="myelement" url="http://site.com/path/config?myname=user">
>
> But you can't use an un-escaped quote mark there either:
>
> torch% print -l ${(s:":)string}
> braceparam dquote>
>
> --
> Barton E. Schaefer
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-05-06  7:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-06  6:28 Splitting on unquoted equals Jesper Nygårds
2015-05-06  7:06 ` Bart Schaefer
2015-05-06  7:25   ` Jesper Nygårds

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).