Pattern Matching Tips
Grep Regular Expression | ||
^ | Denotes the beginning of a line | |
$ | Denotes the end of a line | |
. | Matches any one characters | |
* | Matches 0 or more of the previous characters | |
.* | Matches any number or type of characters | |
[] | Matches on character for the one listed in the the Square brackets | |
[^] | Does not match any characters listed | |
<, >/ | Denotes the beginning and end (respectively) of a word |
So an example of a regular expression search would be
% grep "<[A-Za-z].*" file
This will search for any word which begins with a letter upper or lower case.
Sed Replace Expression
To remove the comments from an apt sources.list file use:
sudo sed -i -e "s/# deb/deb/g" /etc/apt/sources.list
Regular Expression Operators | ||
|
|
a? | matches 0 or 1 occurrence of *a* | 'a' or empty string |
---|---|---|
a* | matches 0 or more occurrences of *a* | empty string or 'a', 'aa', 'aaa', etc |
a+ | matches 1 or more occurrences of *a* | 'a', 'aa', 'aaa', etc |
a|b | match *a* or *b* | 'a' or 'b' - |
. | match any single character | 'a', 'q', 'l', '_', '+', etc |
[woeirjsd] | match any of the named characters | 'w', 'o', 'e', 'i', 'r', 'j', 's', 'd' |
[1-9] | match any of the characters in the range | '1', '2', '3', '4', '5', '6', '7', '8', '9' |
[^13579] | match any characters not named | even digits, or any other character |
(ie) | group an expression (for use with other operators) | 'ie' |
^a | match an *a* at the beginning of a line | 'a' |
a$ | match an *a* at the end of a line | 'a' |
Example:
/ | [^?] | + | ? | / |
---|---|---|---|---|
begin expression | any character other than '?' | more than one of those | a question mark | end expression |
So the use of the in front of the ? makes it refer to an actual question mark.
No comments:
Post a Comment