[comp.unix.questions] Would somebody please explain?

rouben@math16.math.umbc.edu (Rouben Rostamian) (04/21/91)

The command:
    expr "Match" : "Ma"
returns 2, as it should, since the first two characters of "Match" and "Ma"
coincide.  Now, would somebody please explain why the command:
    expr "match" : "ma"
complains of a "syntax error"?

This happens on DEC ultrix (both in sh and csh,) on SUNs, and on Stardent.
I don't claim that this is a bug;  I suspect that I am doing something
stupid.  Please enlighten.

--
Rouben Rostamian                          Telephone: (301) 455-2458
Department of Mathematics and Statistics  e-mail:
University of Maryland Baltimore County   bitnet: rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.              internet: rouben@math9.math.umbc.edu

rickert@mp.cs.niu.edu (Neil Rickert) (04/21/91)

In article <1991Apr21.001702.5661@umbc3.umbc.edu> rouben@math16.math.umbc.edu (Rouben Rostamian) writes:
>The command:
>    expr "Match" : "Ma"
>returns 2, as it should, since the first two characters of "Match" and "Ma"
>coincide.  Now, would somebody please explain why the command:
>    expr "match" : "ma"
>complains of a "syntax error"?

  I just ran:  strings /bin/expr

  From the output I would guess that 'match', 'index', 'substr' and 'length'
are builin functions or operators for 'expr', although they are not documented
in any man pages that I have seen.

  I don't know the syntax for using them either.

  I guess this is one of those questions to cross post to wizards.

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115                                   +1-815-753-6940

henry@angel.Eng.Sun.COM (Henry McGilton) (04/21/91)

In article <1991Apr21.023416.5795@mp.cs.niu.edu>, rickert@mp.cs.niu.edu (Neil Rickert) writes:
    *  In article <1991Apr21.001702.5661@umbc3.umbc.edu> rouben@math16.math.umbc.edu (Rouben Rostamian) writes:

        **  The command:
        **        expr "Match" : "Ma"
	**  returns 2, as it should, since the first two characters
	**  of "Match" and "Ma" coincide.  Now, would somebody please
        **  explain why the command:
	**        expr "match" : "ma"
        **  complains of a "syntax error"?

    *  I just ran:  strings /bin/expr
    *  From the output I would guess that 'match', 'index', 'substr' and
    *  'length' are builin functions or operators for 'expr', although
    *  they are not documented in any man pages that I have seen.
    *  I don't know the syntax for using them either.
    *  I guess this is one of those questions to cross post to wizards.
I hope not.  From SunOS  expr  manual page (last updated 5 January 1988):

    ``Note: the match, substr, length, and index operators cannot
      themselves be used as ordinary strings.''

	........  Henry

ric@optima.UUCP (Ric Anderson) (04/21/91)

From article <1991Apr21.023416.5795@mp.cs.niu.edu>, by rickert@mp.cs.niu.edu (Neil Rickert):
> In article <1991Apr21.001702.5661@umbc3.umbc.edu> rouben@math16.math.umbc.edu (Rouben Rostamian) writes:
[stuff deleted]
>>coincide.  Now, would somebody please explain why the command:
>>    expr "match" : "ma"
>>complains of a "syntax error"?
> 
>   I just ran:  strings /bin/expr
> 
>   From the output I would guess that 'match', 'index', 'substr' and 'length'
> are builin functions or operators for 'expr', although they are not documented
> in any man pages that I have seen.

According to the man page on my system, the syntax of these
functions is :
string : re
match string re
	look in string for the regular expression "re".  returns
	the number of characters matched, or the string matched if
	\(...\) is used.

substr string integer-1 integer-2
	return the substring of "string" starting at integer-1,
	of length integer-2 characters.  1 = first character, and
	a length greater than whats left, returns whats left.

index string list
	return the position of the first character in "string" that
	occurs in "list"

length string
	return the length of "string".

Ric (ric@cs.arizona.edu <Ric Anderson>)

xtdn@levels.sait.edu.au (04/21/91)

rouben@math16.math.umbc.edu (Rouben Rostamian) writes:
>The command:
>    expr "Match" : "Ma"
>returns 2, as it should, since the first two characters of "Match" and "Ma"
>coincide.  Now, would somebody please explain why the command:
>    expr "match" : "ma"
>complains of a "syntax error"?

It is, as you suggest, because you are using a builtin command incorrectly.
I forget the exact details now, but I used often to suffer from similar
problems with arguments that looked like numeric expressions.  Anyway, the
point is that if you prefix the argument with something that makes it look
like nothing special, you won't suffer this type of problem.

So, for example, use:
   expr "xmatch" : "xma" - 1

David Newall, 16:32:56.04, Tuesday, 1991     Phone:  +61 8 344 2008
"Life is uncertain: Eat dessert first"       E-mail: xtdn@lux.sait.edu.au

c60b-1eq@e260-1a.berkeley.edu (Noam Mendelson) (04/21/91)

In article <1991Apr21.001702.5661@umbc3.umbc.edu> rouben@math16.math.umbc.edu (Rouben Rostamian) writes:
>The command:
>    expr "Match" : "Ma"
>returns 2, as it should, since the first two characters of "Match" and "Ma"
>coincide.  Now, would somebody please explain why the command:
>    expr "match" : "ma"
>complains of a "syntax error"?

Remember that 'expr string : regexp' is a pseudonym for
'expr match string regexp'.  When you use "match" as the first argument,
expr expects two additional arguments (i.e., expr is interpreting the
"match" as a command).

-- 
+==========================================================================+
| Noam Mendelson   ..!ucbvax!web!c60b-1eq       | "I haven't lost my mind, |
| c60b-1eq@web.Berkeley.EDU                     |  it's backed up on tape  |
| University of California at Berkeley          |  somewhere."             |

edotto@ux1.cso.uiuc.edu (Ed Otto) (04/22/91)

rouben@math16.math.umbc.edu (Rouben Rostamian) writes:

>The command:
>    expr "Match" : "Ma"
>returns 2, as it should, since the first two characters of "Match" and "Ma"
>coincide.  Now, would somebody please explain why the command:
>    expr "match" : "ma"
>complains of a "syntax error"?

Weird...It wouldn't work with ANY lower case characters EXCEPT with a capital
letter in the front of the strings...

I wanna know, too!


*******************************************************************************
*                             *  Netmail addresses:                           *
*  Edward C. Otto III         *    edotto@uipsuxb.ps.uiuc.edu                 *
*  University of Illinois     *    edotto@uiucux1.cso.uiuc.edu                *
*  Printing Services Office   *    UIPSA::OTTO (Decnet node 46.99)            *
*  54A E. Gregory Dr.         *    otto@uipsa.dnet.nasa.gov                   *
*  Champaign, IL  61820       *  Office phone: 217/333-9422                   *
*                             *                                               *
*******************************************************************************

	"As knowledge is to ignorance, so is light unto the darkness."

		       ---     GO 'PODS!     ---
-- 
*******************************************************************************
*                             *  Netmail addresses:                           *
*  Edward C. Otto III         *    edotto@uipsuxb.ps.uiuc.edu                 *
*  University of Illinois     *    edotto@uiucux1.cso.uiuc.edu                *

guy@auspex.auspex.com (Guy Harris) (04/23/91)

>  From the output I would guess that 'match', 'index', 'substr' and 'length'
>are builin functions or operators for 'expr',

They are.

>although they are not documented in any man pages that I have seen.

The original V7 "expr" had all of 'em built in.  The SunOS 4.0.3 manual
page documents them:

     string : regular-expression
     match string regular-expression
          The two  forms  of  the  matching  operator  above  are
          synonymous.  The matching operators : and match compare
          the first argument with the second argument which  must
          be  a regular expression.  Regular expression syntax is
          the same as that of ed(1), except that all patterns are
          anchored  (treated  as  if  they  begin  with  ^)  and,
          therefore, ^ is not a special character, in  that  con-
          text.   Normally,  the  matching  operator  returns the
          number of characters matched (0 on failure).   Alterna-
          tively,  the  \(...\)  pattern  symbols  can be used to
          return a portion of the first argument.

     substr string integer-1 integer-2
          Extract the subtring of  string  starting  at  position
          integer-1  and  of  length  integer-2  characters.   If
          integer-1 has  a  value  greater  than  the  length  of
          string,  expr  returns  a  null  string.  If you try to
          extract more characters than there are in string,  expr
          returns  all  the  remaining  characters  from  string.
          Beware of using negative values for either integer-1 or
          integer-2 as expr tends to run forever in these cases.

     index string character-list
          Report the first position in string at which any one of
          the characters in character-list matches a character in
          string.

     length string
          Return the length (that is, the number  of  characters)
          of string.

The S5 version ripped "substr", "index", and "length" out - probably to
avoid the problem reported in the original posting - but kept "match" as
an alias for ":", for reasons not obvious to me (considering that doing
so means that the problem reported in the original posting is *NOT*
avoided).

cliff@demon.co.uk (Cliff Stanford) (04/23/91)

In article <1991Apr21.023416.5795@mp.cs.niu.edu> rickert@mp.cs.niu.edu (Neil Rickert) writes:
>In article <1991Apr21.001702.5661@umbc3.umbc.edu> rouben@math16.math.umbc.edu (Rouben Rostamian) writes:
>>The command:
>>    expr "Match" : "Ma"
>>returns 2, as it should, since the first two characters of "Match" and "Ma"
>>coincide.  Now, would somebody please explain why the command:
>>    expr "match" : "ma"
>>complains of a "syntax error"?
>
>  I just ran:  strings /bin/expr
>
>  From the output I would guess that 'match', 'index', 'substr' and 'length'
>are builin functions or operators for 'expr', although they are not documented
>in any man pages that I have seen.
>

	Well I'm blowed!  All those work although I've not seen any
documentation for any one of them. (SCO 3.2.2)

Input						Output
-----						------
expr match abcde abc				3
expr index abcde cd				3
expr length abcde				5
expr substr abcde 2 3				bcd

	Why doesn't anyone DOCUMENT unix?
		Cliff.
-- 
Cliff Stanford				Email:	cliff@demon.co.uk (Work)
Demon Systems Limited				cms@demon.co.uk   (Home)
42 Hendon Lane				Phone:	081-349 0063	  (Office)
London	N3 1TT	England				0860 375870	  (Mobile)

peter@ficc.ferranti.com (Peter da Silva) (04/25/91)

In article <1991Apr22.230906.9239@demon.co.uk> cliff@demon.co.uk (Cliff Stanford) writes:
> 	Why doesn't anyone DOCUMENT unix?
> 		Cliff.

Because that would take all the FUN out of it!
-- 
Peter da Silva.  `-_-'  peter@ferranti.com
+1 713 274 5180.  'U`  "Have you hugged your wolf today?"