Regular expressions: Difference between revisions

Line 67:
Gives 101 dogs
</pre>
 
=={{header|AWK}}==
AWK supports regular expressions, which are typically marked up with slashes in front and back, and the "~" operator:
$ awk '{if($0~/[A-Z]/)print "uppercase detected"}'
abc
ABC
uppercase detected
As shorthand, a regular expression in the condition part fires if it matches an input line:
awk '/[A-Z]/{print "uppercase detected"}'
def
DeF
uppercase detected
For substitution, the first argument can be a regular expression, while the replacement string is constant (only that '&' in it receives the value of the match):
$ awk '{gsub(/[A-Z]/,"*");print}'
abCDefG
ab**ef*
$ awk '{gsub(/[A-Z]/,"(&)");print}'
abCDefGH
ab(C)(D)ef(G)(H)
This variant matches one or more uppercase letters in one round:
$ awk '{gsub(/[A-Z]+/,"(&)");print}'
abCDefGH
ab(CD)ef(GH)
 
=={{header|C}}==
Anonymous user