zsh-users
 help / color / mirror / code / Atom feed
* how to search for a substring in a parameter?
@ 2003-01-13 18:13 Carlos Carvalho
  2003-01-13 18:26 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Carlos Carvalho @ 2003-01-13 18:13 UTC (permalink / raw)
  To: zsh-users

I'm trying to see if the value of a parameter contains a given string
but didn't find an elegant way. All pattern matching substitutions
return only the non-matched portion of the value. There's a way to do
substitutions but not testing. I must be missing something. I'm using

   if (( ${#foo//pattern/} < ${#foo} )); then
	# contains pattern
   else
	# doesn't contain pattern
   fi

Is there a better way?


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

* Re: how to search for a substring in a parameter?
  2003-01-13 18:13 how to search for a substring in a parameter? Carlos Carvalho
@ 2003-01-13 18:26 ` Bart Schaefer
  2003-01-13 18:26 ` Zefram
  2003-01-13 18:32 ` Peter Stephenson
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2003-01-13 18:26 UTC (permalink / raw)
  To: zsh-users

On Jan 13,  4:13pm, Carlos Carvalho wrote:
}
} I'm trying to see if the value of a parameter contains a given string

The right-hand-side of == in a [[ ]] construct is a glob pattern, so if
all you want is true/false, this works:

    if [[ $foo == *pattern* ]]; then
 	# contains pattern
    else
 	# doesn't contain pattern
    fi

If you want to extract the matching string, see the M and S flags:

    matching=${(SM)foo##pattern}


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

* Re: how to search for a substring in a parameter?
  2003-01-13 18:13 how to search for a substring in a parameter? Carlos Carvalho
  2003-01-13 18:26 ` Bart Schaefer
@ 2003-01-13 18:26 ` Zefram
  2003-01-13 18:32 ` Peter Stephenson
  2 siblings, 0 replies; 6+ messages in thread
From: Zefram @ 2003-01-13 18:26 UTC (permalink / raw)
  To: Carlos Carvalho; +Cc: zsh-users

Carlos Carvalho wrote:
>I'm trying to see if the value of a parameter contains a given string
>but didn't find an elegant way.

	if [[ $parameter == *given_string* ]]; then
		# yes
	else
		# no
	fi

-zefram


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

* Re: how to search for a substring in a parameter?
  2003-01-13 18:13 how to search for a substring in a parameter? Carlos Carvalho
  2003-01-13 18:26 ` Bart Schaefer
  2003-01-13 18:26 ` Zefram
@ 2003-01-13 18:32 ` Peter Stephenson
  2003-01-13 21:48   ` Carlos Carvalho
  2 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2003-01-13 18:32 UTC (permalink / raw)
  To: zsh-users

Carlos Carvalho wrote:
> I'm trying to see if the value of a parameter contains a given string
> but didn't find an elegant way.

What was wrong with

if [[ $foo = *pattern* ]]; then
  ...

?  The patterns are identical to those in parameter substitution.

> All pattern matching substitutions
> return only the non-matched portion of the value.

You can use the flags M for the matched portion combined with either
head or tail matching using the S flag to tell it to match substrings,
i.e. anywhere in the pattern.

% foo='this is a pattern, too'
% print ${(MS)foo##pattern}
pattern

The M flag doesn't work with the `/' or `//' substitutions (and the S
flag is differnt --- it forces the shortest match).  Not clear
if that's `really' a bug, but it does agree with the documentation.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: how to search for a substring in a parameter?
  2003-01-13 18:32 ` Peter Stephenson
@ 2003-01-13 21:48   ` Carlos Carvalho
  2003-01-14  5:16     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Carlos Carvalho @ 2003-01-13 21:48 UTC (permalink / raw)
  To: zsh-users

Sorry for making noise with such an easy question... I even found the
answer myself some time later but you were too fast in answering for
me to tell it. Anyway I now understand the meaning of M, thanks.

Fascinated by that part of the man page I tried the following:

% max=8
% bar=5
% if [[ $bar == <1-$max> ]] {print in } else {print out}
zsh: parse error: condition expected: $bar

It works if I change $max by 8. Is it illegal to have parameters
inside <-> or I'm doing it wrong? The man page is a little ambiguous.


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

* Re: how to search for a substring in a parameter?
  2003-01-13 21:48   ` Carlos Carvalho
@ 2003-01-14  5:16     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2003-01-14  5:16 UTC (permalink / raw)
  To: zsh-users

On Jan 13,  7:48pm, Carlos Carvalho wrote:
}
} % if [[ $bar == <1-$max> ]] {print in } else {print out}
} zsh: parse error: condition expected: $bar
} 
} Is it illegal to have parameters inside <-> ...?

Yes.  Do it this way:

range='<1-8>'

if [[ $bar == ${~range} ]] {print in} else {print out}


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

end of thread, other threads:[~2003-01-14  5:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-13 18:13 how to search for a substring in a parameter? Carlos Carvalho
2003-01-13 18:26 ` Bart Schaefer
2003-01-13 18:26 ` Zefram
2003-01-13 18:32 ` Peter Stephenson
2003-01-13 21:48   ` Carlos Carvalho
2003-01-14  5:16     ` 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).