regular expression operator

The awk programming language provides a set of *regular expression operators* that have special meanings within regular expressions.

_Regular Expression Binding Operators_

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 when a regular expression is used as a pattern within a rule_

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}

_Substitution cannot be performed with binding operators_

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
 }

_Basic Regular Expression Operators_

*Char**Name**Purpose*
^[[caret?]]Anchor matches the beginning of a string
$dollarAnchor matches the end of a string
.dotMatches any single character including a newline

_Extended Regular Expression Operators_

*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