Strong Regexp Constants (The GNU Awk User’s Guide)

From Get docs
Gawk/docs/latest/Strong-Regexp-Constants


6.1.2.2 Strongly Typed Regexp Constants

This section describes a gawk-specific feature.

As we saw in the previous section, regexp constants (/…/) hold a strange position in the awk language. In most contexts, they act like an expression: ‘$0 ~ /…/’. In other contexts, they denote only a regexp to be matched. In no case are they really a “first class citizen” of the language. That is, you cannot define a scalar variable whose type is “regexp” in the same sense that you can define a variable to be a number or a string:

num = 42        Numeric variable
str = "hi"      String variable
re = /foo/      Wrong! re is the result of $0 ~ /foo/

For a number of more advanced use cases, it would be nice to have regexp constants that are strongly typed; in other words, that denote a regexp useful for matching, and not an expression.

gawk provides this feature. A strongly typed regexp constant looks almost like a regular regexp constant, except that it is preceded by an ‘@’ sign:

re = @/foo/     Regexp variable

Strongly typed regexp constants cannot be used everywhere that a regular regexp constant can, because this would make the language even more confusing. Instead, you may use them only in certain contexts:

  • On the righthand side of the ‘~’ and ‘!~’ operators: ‘some_var ~ @/foo/’ (see section How to Use Regular Expressions).
  • In the case part of a switch statement (see section The switch Statement).
  • As an argument to one of the built-in functions that accept regexp constants: gensub(), gsub(), match(), patsplit(), split(), and sub() (see section String-Manipulation Functions).
  • As a parameter in a call to a user-defined function (see section User-Defined Functions).
  • On the righthand side of an assignment to a variable: ‘some_var = @/foo/’. In this case, the type of some_var is regexp. Additionally, some_var can be used with ‘~’ and ‘!~’, passed to one of the built-in functions listed above, or passed as a parameter to a user-defined function.

You may use the typeof() built-in function (see section Getting Type Information) to determine if a variable or function parameter is a regexp variable.

The true power of this feature comes from the ability to create variables that have regexp type. Such variables can be passed on to user-defined functions, without the confusing aspects of computed regular expressions created from strings or string constants. They may also be passed through indirect function calls (see section Indirect Function Calls) and on to the built-in functions that accept regexp constants.

When used in numeric conversions, strongly typed regexp variables convert to zero. When used in string conversions, they convert to the string value of the original regexp text.