Easily find issues by searching: #<Issue ID>
Example: #1832
Easily find members by searching in: <username>, <first name> and <last name>.
Example: Search smith, will return results smith and adamsmith
An expression is a string of characters. Those characters having an interpretation above and beyond their literal meaning are called metacharacters. A quote symbol, for example, may denote speech by a person, ditto, or a meta-meaning 1 for the symbols that follow. Regular Expressions are sets of characters and/or metacharacters that match (or specify) patterns.
A Regular Expression contains one or more of the following:
The main uses for Regular Expressions (REs) are text searches and string manipulation. An RE matches a single character or a set of characters -- a string or a part of a string.
*
-- matches any number of repeats of the character string or RE preceding it, including zero instances. "13." matches 13 + at least one of any character (including a space): 1133, 11333, but not 13 (additional character missing).
See Example 16-18 for a demonstration of dot single-character matching.
The caret -- ^ -- matches the beginning of a line, but sometimes, depending on context, negates the meaning of a set of characters in an RE.
The dollar sign -- $ -- at the end of an RE matches the end of a line.
"XXX$" matches XXX at the end of a line.
"^$" matches blank lines.
Brackets -- [...] -- enclose a set of characters to match in a single RE.
"[xyz]" matches any one of the characters x, y, or z.
"[c-n]" matches any one of the characters in the range c to n.
"[B-Pk-y]" matches any one of the characters in the ranges B to P and k to y.
"[a-z0-9]" matches any single lowercase letter or any digit.
"[^b-d]" matches any character except those in the range b to d. This is an instance of ^ negating or inverting the meaning of the following RE (taking on a role similar to ! in a different context).
Combined sequences of bracketed characters match common word patterns. "[Yy][Ee][Ss]" matches yes, Yes, YES, yEs, and so forth. "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" matches any Social Security number.
The backslash -- \ -- escapes a special character, which means that character gets interpreted literally (and is therefore no longer special).
A "\$" reverts back to its literal meaning of "$", rather than its RE meaning of end-of-line. Likewise a "\\" has the literal meaning of "\".
Escaped "angle brackets" -- \<...\> -- mark word boundaries.
The angle brackets must be escaped, since otherwise they have only their literal character meaning.
"\<the\>" matches the word "the," but not the words "them," "there," "other," etc.
bash$ cat textfile This is line 1, of which there is only one instance. This is the only instance of line 2. This is line 3, another line. This is line 4. bash$ grep 'the' textfile This is line 1, of which there is only one instance. This is the only instance of line 2. This is line 3, another line. bash$ grep '\<the\>' textfile This is the only instance of line 2.
The only way to be certain that a particular RE works is to test it.
TEST FILE: tstfile # No match. # No match. Run grep "1133*" on this file. # Match. # No match. # No match. This line contains the number 113. # Match. This line contains the number 13. # No match. This line contains the number 133. # No match. This line contains the number 1133. # Match. This line contains the number 113312. # Match. This line contains the number 1112. # No match. This line contains the number 113312312. # Match. This line contains no numbers at all. # No match.
bash$ grep "1133*" tstfile Run grep "1133*" on this file. # Match. This line contains the number 113. # Match. This line contains the number 1133. # Match. This line contains the number 113312. # Match. This line contains the number 113312312. # Match.
# GNU versions of sed and awk can use "+", # but it needs to be escaped. echo a111b | sed -ne '/a1\+b/p' echo a111b | grep 'a1\+b' echo a111b | gawk '/a1+b/' # All of above are equivalent. # Thanks, S.C.
Curly brackets are not available as an RE in the "classic" (non-POSIX compliant) version of awk. However, the GNU extended version of awk, gawk, has the --re-interval option that permits them (without being escaped).
bash$ echo 2222 | gawk --re-interval '/2{3}/' 2222
Perl and some egrep versions do not require escaping the curly brackets.
POSIX Character Classes. [:class:]
This is an alternate method of specifying a range of characters to match.
bash$ grep [[:digit:]] test.file abc=723
# ... if [[ $arow =~ [[:digit:]] ]] # Numerical input? then # POSIX char class if [[ $acol =~ [[:alpha:]] ]] # Number followed by a letter? Illegal! # ... # From ktour.sh example script.
These character classes may even be used with globbing, to a limited extent.
bash$ ls -l ?[[:digit:]][[:digit:]]? -rw-rw-r-- 1 bozo bozo 0 Aug 21 14:47 a33b
POSIX character classes are used in Example 16-21 and Example 16-22.
Sed, awk, and Perl, used as filters in scripts, take REs as arguments when "sifting" or transforming files or I/O streams. See Example A-12 and Example A-16 for illustrations of this.
The standard reference on this complex topic is Friedl's Mastering Regular Expressions. Sed & Awk, by Dougherty and Robbins, also gives a very lucid treatment of REs. See the Bibliography for more information on these books.
#!/bin/bash sed -e 'N;s/.*/[&]/' << EOF # Here Document line1 line2 EOF # OUTPUT: # [line1 # line2] echo awk '{ $0=$1 "\n" $2; if (/line.1/) {print}}' << EOF line 1 line 2 EOF # OUTPUT: # line # 1 # Thanks, S.C. exit 0
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017