Regular Expressionsby qbguy (Login dean.menezes)Linux-Forum Regular expressions can be used to search for text that matches a certain pattern rather than an exact string. They can be used in many command-line tools such as (e)grep, sed, awk and are also a feature of many programming languages.
Any non-metacharacter, or a metacharacter (one of .*+?[]()|\^$), preceded by a backslash (\) matches itself. [s] matches any of the characters inside the brackets. (e.g. [a-z] means any lowercase letter). [^s] matches any character NOT inside the brackets. (e.g. [^a-z] means anything that isn't a lowercase letter). You can include dashes and ] inside the bracket expression by preceding them with a backslash. A . matches any character. A ^ means the expression has to be at the beginning of a line; a $ means it has to be at the end of the line. * means zero or more of the previous expression. + means one or more. ? means zero or one. | can be put between two regexes to mean either the first or the second. Expressions in parenthesis can be retrieved later (in sed, etc) by using \1 \2 \3 (\n for the nth one). Also, braces may be supported as a shortcut to specify multiple matches (e.g {5,} is 5+ matches or {1,5} is between 1 and 5 matches). === Operator precedence === Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules.
|