OR in `expr match`
Your command should be:
expr match Unauthenticated123 'Unauthenticated\|Authenticated'
If you want the number of characters matched.
To have the part of the string (Unauthenticated) returned use:
expr match Unauthenticated123 '\(Unauthenticated\|Authenticated\)'
From info coreutils 'expr invocation'
:
`STRING : REGEX' Perform pattern matching. The arguments are converted to strings and the second is considered to be a (basic, a la GNU `grep') regular expression, with a `^' implicitly prepended. The first argument is then matched against this regular expression. If the match succeeds and REGEX uses `\(' and `\)', the `:' expression returns the part of STRING that matched the subexpression; otherwise, it returns the number of characters matched. If the match fails, the `:' operator returns the null string if `\(' and `\)' are used in REGEX, otherwise 0. Only the first `\( ... \)' pair is relevant to the return value; additional pairs are meaningful only for grouping the regular expression operators. In the regular expression, `\+', `\?', and `\|' are operators which respectively match one or more, zero or one, or separate alternatives. SunOS and other `expr''s treat these as regular characters. (POSIX allows either behavior.) *Note Regular Expression Library: (regex)Top, for details of regular expression syntax. Some examples are in *note Examples of expr::.
Note that both match
and \|
are GNU extensions (and the behaviour for :
(the match
standard equivalent) when the pattern starts with ^
varies with implementations). Standardly, you'd do:
expr " $string" : " Authenticated" '|' " $string" : " Unauthenticated"
The leading space is to avoid problems with values of $string
that start with -
or are expr
operators, but that means it adds one to the number of characters being matched.
With GNU expr
, you'd write it:
expr + "$string" : 'Authenticated\|Unauthenticated'
The +
forces $string
to be taken as a string even if it happens to be a expr
operator. expr
regular expressions are basic regular expressions which don't have an alternation operator (and where |
is not special). The GNU implementation has it as \|
though as an extension.
If all you want is to check whether $string
starts with Authenticated
or Unauthenticated
, you'd better use:
case $string in
(Authenticated* | Unauthenticated*) do-something
esac
$ expr match "Unauthenticated123" '^\(Unauthenticated\|Authenticated\).*'
you have to escape with \
the parenthesis and the pipe.