coreutils: String expressions
1
1 16.4.1 String expressions
1 -------------------------
1
1 ‘expr’ supports pattern matching and other string operators. These have
1 higher precedence than both the numeric and relational operators (in the
1 next sections).
1
1 ‘STRING : REGEX’
1 Perform pattern matching. The arguments are converted to strings
1 and the second is considered to be a (basic, a la GNU ‘grep’)
1 regular expression, with a ‘^’ implicitly prepended. The first
1 argument is then matched against this regular expression.
1
1 If the match succeeds and REGEX uses ‘\(’ and ‘\)’, the ‘:’
1 expression returns the part of STRING that matched the
1 subexpression; otherwise, it returns the number of characters
1 matched.
1
1 If the match fails, the ‘:’ operator returns the null string if
1 ‘\(’ and ‘\)’ are used in REGEX, otherwise 0.
1
1 Only the first ‘\( ... \)’ pair is relevant to the return value;
1 additional pairs are meaningful only for grouping the regular
1 expression operators.
1
1 In the regular expression, ‘\+’, ‘\?’, and ‘\|’ are operators which
1 respectively match one or more, zero or one, or separate
1 alternatives. SunOS and other ‘expr’’s treat these as regular
11 characters. (POSIX allows either behavior.) ⇒Regular
Expression Library (regex)Top, for details of regular expression
1 syntax. Some examples are in ⇒Examples of expr.
1
1 ‘match STRING REGEX’
1 An alternative way to do pattern matching. This is the same as
1 ‘STRING : REGEX’.
1
1 ‘substr STRING POSITION LENGTH’
1 Returns the substring of STRING beginning at POSITION with length
1 at most LENGTH. If either POSITION or LENGTH is negative, zero, or
1 non-numeric, returns the null string.
1
1 ‘index STRING CHARSET’
1 Returns the first position in STRING where the first character in
1 CHARSET was found. If no character in CHARSET is found in STRING,
1 return 0.
1
1 ‘length STRING’
1 Returns the length of STRING.
1
1 ‘+ TOKEN’
1 Interpret TOKEN as a string, even if it is a keyword like MATCH or
1 an operator like ‘/’. This makes it possible to test ‘expr length
1 + "$x"’ or ‘expr + "$x" : '.*/\(.\)'’ and have it do the right
1 thing even if the value of $X happens to be (for example) ‘/’ or
1 ‘index’. This operator is a GNU extension. Portable shell scripts
1 should use ‘" $token" : ' \(.*\)'’ instead of ‘+ "$token"’.
1
1 To make ‘expr’ interpret keywords as strings, you must use the
1 ‘quote’ operator.
1