Although we’ve concentrated on using character classes for matching, there are cases where this may not be appropriate. In this example we will restrict the letter the user name starts with: “u”, “a”, or “s”.

( _my_command_c_option ! ~ "^[uas][[:digit:]]+[[:digit:]]$” ) { error+=”The user name must start with u, a, or s followed by only digits”

} else {

note+=”The user name chosen was “ + ${_my_command_c_option}

}

So instead of allowing any upper or lower case letter to start the user name, we now need one of: “u”, “a”, or “s”.

It is important to understand what bracket expressions match. A bracket expression is used to match a single character. Look at the following test:

$ cat data ab000123 cd000243 ad999233 ef000033 gh888444

$ grep -E "^[acg][bdh][[:digit:]]+[[:digit:]]$" data ab000123

cd000243

ad999233

gh888444

$ grep -E "^(abcdgh)[[:digit:]]+[[:digit:]]$" data ab000123

cd000243

gh888444

The bracket expressions [acg][bdh] mean: match one character, “a”, “c”, or “g”, followed by one character, “b”, “d”, or “h”. If you wish to match something containing “ab”, “cd”, or “gh” then a bracket expression is not what should be used.

In that case you can use parentheses with the “or” operator to group expressions together, so any of the expressions are used for matching. In the following example we have (abcdgh), which matches “ab”, “cd”, or “gh”. The parentheses are important. If you didn’t have them, the following extended regular expression:

"^abcdgh[[:digit:]]+[[:digit:]]$"

would match:1.Something that starts with “ab“2.Something that contains “cd“

3.Something that ends with the pattern “gh“ followed by one or more numbers, and ends in a number (since it is anchored to the end of what is being matched against).

So the following expression tests for a user name that starts with “ab”, “cd”, or “gh” followed by one or more numbers and ends in a number:

147