manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Strong Regexp Constants Strongly Typed Regexp Constants
 This minor node describes a 'gawk'-specific feature.
    As we saw in the previous minor node, 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/' (⇒Regexp Usage).
    * In the 'case' part of a 'switch' statement (⇒Switch
    * As an argument to one of the built-in functions that accept regexp
      constants: 'gensub()', 'gsub()', 'match()', 'patsplit()',
      'split()', and 'sub()' (⇒String Functions).
    * As a parameter in a call to a user-defined function (⇒
    * 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 (⇒Type Functions)
 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 (⇒Indirect 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.
© 2000-2018
Individual documents may contain additional copyright information.