

The pattern b.g$ would match "big" or "bag" only if they occur at the end of the line, but not "bigger." The expression ^b.g would only match "big," "bigger," "bag," etc., as shown above if they occur at the beginning of the line being parsed. They are said to anchor the rest of the pattern to either the beginning or end of a line. These two metacharacters match the beginning and ending of a line, respectively. Note that there may be issues due to differences in the sorting sequences in different locales. Other classes are which matches any one digit 0-9,, , and so on. For example, the class matches all alphanumeric characters. The class names are supposed to be obvious. This is a POSIX attempt at regex standardization. matches the alphabet in upper- and lowercase. specifies a range of characters that creates the same match. matches the letters "a," b," "c," or "d" in either upper- or lowercase. The brackets enclose a list of characters to match for a single character location in the pattern. GNU grep calls this a bracket expression, and it is the same as a set for the Bash shell. So the pattern b.g would match "big," "bigger," "bag," "baguette," and "bog," but not "dog," "blog," "hug," "lag," "gag," "leg," etc. It matches any single character in the position it is encountered in a pattern. The dot (.) metacharacter is the most basic form of expression. Each literal character matches one and only one character. There is no ambiguity for these characters. Thus the letter a in a regex will always match the letter "a" in the data stream being parsed. When used in a pattern, each of these expressions or metacharacters matches a single character in the data stream being parsed: ExpressionĪll alphanumeric and some punctuation characters are considered as literals. The following table contains a list of the basic building block expressions and metacharacters implemented by the GNU grep command (and most other regex implementations), and their descriptions. When a match is made, an action defined by the tool in use is taken on the line that contains the matching string. Think of each data stream line as a record, where the tools that use regexes process one record at a time.

As we saw in Getting started with regular expressions: An example, the -v option reverses those actions, so that the lines with matches are discarded.Įach line of the data stream is evaluated on its own. When a match is made, an action is taken on that line as prescribed by the tool being used.įor example, when a pattern match occurs with grep, the usual action is to pass that line to STDOUT and discard lines that do not match the pattern.

A pattern created by a combination of one or more expressions is compared against each line of a data stream. How well do you know Linux? Take a quiz and get a badgeĪll implementations of regular expressions are line-based.Linux system administration skills assessment.A guide to installing applications on Linux.
#Grep regex at least a type of characters download#
Download RHEL 9 at no charge through the Red Hat Developer program.
