zsh-workers
 help / color / mirror / code / Atom feed
* Regexp replace on all arguments.
@ 2006-02-11 17:36 Ligesh
  2006-02-11 17:38 ` Stephane Chazelas
  0 siblings, 1 reply; 4+ messages in thread
From: Ligesh @ 2006-02-11 17:36 UTC (permalink / raw)
  To: Zsh-workers


 Hi folks,

  I want to replace all occurrences of say '/c/' in the arguments with 'c:'. Could someone help me with completing the function below.

  winexec () {

	  // First loop through all arguments and replace

	  foreach arguments $2 to $- {
		  replace ^/c/ with c: , ^/d/ with d: etc. (The character c, d should be preserved, '^' means beginning of the word.)
	  }

	  execute $1 with the new arguments.
	  
  }

  The execution would be

  $ winexec cacls.exe /c/name-of-file

  Now the function should change it to:

  $ calcs.exe c:/name-of-file

  Thanks in advance.


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

* Re: Regexp replace on all arguments.
  2006-02-11 17:36 Regexp replace on all arguments Ligesh
@ 2006-02-11 17:38 ` Stephane Chazelas
  2006-02-11 18:21   ` Ligesh
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2006-02-11 17:38 UTC (permalink / raw)
  To: Zsh-workers

On Sat, Feb 11, 2006 at 11:06:26PM +0530, Ligesh wrote:
> 
>  Hi folks,
> 
>   I want to replace all occurrences of say '/c/' in the arguments with 'c:'. Could someone help me with completing the function below.
> 
>   winexec () {
> 
> 	  // First loop through all arguments and replace
> 
> 	  foreach arguments $2 to $- {
> 		  replace ^/c/ with c: , ^/d/ with d: etc. (The character c, d should be preserved, '^' means beginning of the word.)
> 	  }
> 
> 	  execute $1 with the new arguments.
> 	  
>   }
> 
>   The execution would be
> 
>   $ winexec cacls.exe /c/name-of-file
[...]

winexec() {
  local cmd=$1
  shift || return
  argv=("${@//#\/(#b)([a-zA-Z])\//$match:}")
  "$cmd" "$@"
}

-- 
Stéphane


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

* Re: Regexp replace on all arguments.
  2006-02-11 17:38 ` Stephane Chazelas
@ 2006-02-11 18:21   ` Ligesh
  2006-02-11 19:20     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Ligesh @ 2006-02-11 18:21 UTC (permalink / raw)
  To: Zsh-workers

On Sat, Feb 11, 2006 at 05:38:19PM +0000, Stephane Chazelas wrote:
> winexec() {
>   local cmd=$1
>   shift || return
>   argv=("${@//#\/(#b)([a-zA-Z])\//$match:}")
>   "$cmd" "$@"
> }
> 

 I couldn't get it to work. There is no substitution happening at all. I am getting the same arguments.

 winexec cacl.exe /c/name-fo-file

 gives: cacls.exec /c/name-of-file.

 I am sure the code is the right direction, and but there is some silly bug which makes it fail. I am actually used to vim/emacs syntax for string substitution, but I found the zsh syntax to be a bit exotic. Especially since zsh has SO many special cases. So if you could explain the above, I would be able to correct it myself.

  argv=("${@//#\/(#b)([a-zA-Z])\//$match:}")

  I take it that the first '@' stands for all arguments.

  What's the /#?
  what's (#b)?
 ([a-zA-Z]) stands for 1 character, enclosed in a paranthesis for its use in the replace regexp.

 Thanks.



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

* Re: Regexp replace on all arguments.
  2006-02-11 18:21   ` Ligesh
@ 2006-02-11 19:20     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2006-02-11 19:20 UTC (permalink / raw)
  To: Zsh-workers

On Feb 11, 11:51pm, Ligesh wrote:
> Subject: Re: Regexp replace on all arguments.
>
> On Sat, Feb 11, 2006 at 05:38:19PM +0000, Stephane Chazelas wrote:
> > winexec() {
> >   local cmd=$1
> >   shift || return
> >   argv=("${@//#\/(#b)([a-zA-Z])\//$match:}")
> >   "$cmd" "$@"
> > }
> > 
>
>  I couldn't get it to work. There is no substitution happening at all.

    winexec () {
        setopt localoptions extendedglob
        local cmd=$1
        shift || return
        argv=("${@//#\/(#b)([a-zA-Z])\//$match:}")
        "$cmd" "$@"
    }

>   What's the /#?

You're parsing it wrong.

 @                      The variable to expand
 //                     Replace all occurrences of
 #\/(#b)([a-zA-Z])\/    this pattern
 /                      with
 $match:                this expansion (see "backreference" below)

>   what's (#b)?

The pattern is

 #                      Anchor at beginning
 \/                     a slash
 (#b)                   activate "backreferences"
 ([a-zA-Z])             create a backreference to one alphabetic
 \/                     another slash

The backreference stuff requires extendedglob.  It's actually not
useful to use the // for replace-all because there can only be one
match when anchored at the beginning, so just ${@/#.../...} would
have been OK.

(This question really should have been asked on zsh-users.)


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

end of thread, other threads:[~2006-02-11 19:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-11 17:36 Regexp replace on all arguments Ligesh
2006-02-11 17:38 ` Stephane Chazelas
2006-02-11 18:21   ` Ligesh
2006-02-11 19:20     ` Bart Schaefer

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).