The awk programming language provides a set of *regular expression operators* that have special meanings within regular expressions.
The *regular expression binding operator* applies the the right hand operand as a regular expression to the left hand operand and return a truth value depending on whether or not a match was found.
BEGIN {
text = "I like banana milkshake"
if (text ~ /banana/) {
print "Match found"
}
}
Negation can be achieved by combining the regular expression binding operator with a logical not operator:
if (text !~ /strawberry/) {
print "Match not found"
}
A binding operator is not required if a regular expression is being used as a pattern within a rule:
# There is no binding operator when a regular expression is used as a pattern
/rhubarb/ {print $0}
Note that the regular expression binding operators cannot perform substitution to the left hand operand:
# This will not work
$text = "I like banana milkshake"
if (text ~ s/banana/chocolate/) {
print "Substitution made"
print text
}
| *Char* | *Name* | *Purpose* |
| ^ | [[caret?]] | Anchor matches the beginning of a string |
| $ | dollar | Anchor matches the end of a string |
| . | dot | Matches any single character including a newline |
| *Char* | *Name* | *Purpose* |
| + | [[plus?]] | The [[plus?]] operator matches an operator or character at least once |
| ? | [[hook?]] | The [[hook?]] operator matches an operator or character either once or not at all |