Basic Regular Expression.(BRE)
This following content is about sed (Stream editor) and how to use regular expression syntax.
Regular expression matcher is greedy, matches are attempted from left to right, and if two or more matches are possible starting at the same character, it selects the longest
Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be:
- an ordinary character.
- a special character preceded by \
- a grouped regular expression
- a bracket expression.
*, but matches one or more.
As *, but only matches exactly zero or one.
As , but matches exactly
i sequences ( *i is a decimal integer; for portability, keep it between 0 and 255 inclusive).
Matches between i and j, inclusive, sequences.
Mathces more than or equal to i sequences.
Groups the inner regexp as a whole.
- Apply postfix operators, like
\(abcd\)*will search zero or more whole sequences of ‘abcd’.
- Use back references.
Matches any character, including newline.
matches the null string at beginning of the pattern space, but ^ will acts as a special character only at the beginning of the regular expression or subexpression (that is, after
It is the same as
^, but refers to end of pattern space.
$ also acts as a special character only at the end of the regular expression or subexpression(that is, before
Matches any single character in list.
\ are normally not special within list
Matches any single character not in the list.
Matches the concatenation of regexp1 and regexp2
\digit (back reference)
Matches the digit-th
\(...\) parenthesized subexpression in the regular expression.
Matches the new line characters.
Matches char，where char is one of
Matches zero or more ‘a’s followed by a single ‘b’. For example, ‘b’ or ‘aaaaab’.
Matches one or more ‘a’s followed by one or more ‘b’s: ‘ab’ is the shortest possible match, but other examples are ‘aaaab’ or ‘abbbbb’ or ‘aaaaaabbbbbbb’.
These two both match all the characters in a string; however, the first matches every string (including the empty string), while the second matches only strings containing at least one character.
This matches a string starting with ‘main’, followed by an opening and closing parenthesis. The
(’ and ‘) need not be adjacent.
This matches a string beginning with ‘#’.
This matches a string ending with a single backslash. The regexp contains two backslashes for escaping.
Instead, this matches a string consisting of a single dollar sign, because it is escaped.
In the C locale, this matches any ASCII letters or digits.
(Here tab stands for a single tab character.) This matches a string of one or more characters, none of which is a space or a tab. Usually this means a word.
This matches a string consisting of two equal substrings separated by a newline.
This matches nine characters followed by an ‘A’.
This matches the start of a string that contains 16 characters, the last of which is an ‘A’.
Extended Regular Expression(ERE)
sed, the only difference between basic and extended regular expressions is in the behavior of a new special characters :
+ , parentheses
BREsyntax, these characters do not have special meaning unless prefixed prefixed backslash
EREsyntax, these characters are special unless they are prefixed with backslash
|literal ‘+’ (plus sign)||*
|one or more ‘a’ characters followed by ‘b’ (plus sign as special meta-character)||