Hi, I am trying to match this word: hexagon-bx.mydomain.com with regex. I have tried this:

Code:

"[a-z0-9\-\[bu]\.*]*$"

So far I have not been successful. I also need to make sure that the regex will match words that just have lowercase letters and numbers in them, such as camera01. How can I create such an expression? I am trying to do a expr match on "[a-z0-9\-\[bu]\.*]*$" so that it would pick up words like hexagon-bx/mydomain.com. Any help is greatly appreciated!

What you proposed will match your requirement. But it will match almost anything else that does not have an upper case or punctuation char at the end. What do you want to discriminate it against? Pls post input and desired output, and the command that you want to run.

Thanks, Scrutinizer! I was actually trying to use if expr match "$xyz" "[a-z0-9.-] and the post you gave worked! $xyz was a variable assigned to hexagon-bu.mydomain.com. The only questions I now have is

1. Does expr match use regex differently than egrep?
2. Why do we not need to escape the period or the dash in the [z-a0-9.*] I am not sure why we would not need to escape these characters as I am relatively a newbie to regex.

No, expr and egrep do not use the same types of regular expression. Although some man pages aren't very clear about what form of regular expressions are used by expr match string pattern and expr string : pattern, the standards are clear:

Code:

The ':' matching operator shall compare the string resulting from the
evaluation of expr1 with the regular expression pattern resulting from
the evaluation of expr2. Regular expression syntax shall be that
defined in the Base Definitions volume of IEEE Std 1003.1-2001, Section
9.3, Basic Regular Expressions, except that all patterns are anchored
to the beginning of the string (that is, only sequences starting at the
first character of a string are matched by the regular expression) and,
therefore, it is unspecified whether '^' is a special character in that
context. Usually, the matching operator shall return a string repre-
senting the number of characters matched ( '0' on failure). Alterna-
tively, if the pattern contains at least one regular expression subex-
pression "[\(...\)]" , the string corresponding to "\1" shall be
returned.

Note that the match string pattern form is not in the standards; it is an extension supported on some (but not all) systems.

The egrep (which is obsolete) and the equivalent grep -E commands use extended regular expressions. The description of grep's regular expressions in POSIX is:

Code:

Regular expression matching shall be based on text lines. Since a <newline> separates or
terminates patterns (see the −e and −f options below), regular expressions cannot contain a
<newline>. Similarly, since patterns are matched against individual lines (excluding the
terminating <newline> characters) of the input, there is no way for a pattern to match a
<newline> found in the input.

and

Code:

-E Match using extended regular expressions. Treat each pattern
specified as an ERE, as described in the Base Definitions volume
of IEEE Std 1003.1-2001, Section 9.4, Extended Regular Expres-
sions. If any entire ERE pattern matches some part of an input
line excluding the terminating <newline>, the line shall be
matched. A null ERE shall match every line.

You don't need to backslash-escape characters in a bracket expression in an RE. If you do, the backslash character itself becomes a member of the set of characters to be matched by a matching bracket expression or to be excluded from the list of characters to be matched by a non-matching bracket expression.