m4: Regexp
1
1 11.3 Searching for regular expressions
1 ======================================
1
1 Searching for regular expressions is done with the builtin 'regexp':
1
1 -- Builtin: regexp (STRING, REGEXP, [REPLACEMENT])
1 Searches for REGEXP in STRING. The syntax for regular expressions
1 is the same as in GNU Emacs, which is similar to BRE, Basic Regular
11 Expressions in POSIX. ⇒Syntax of Regular Expressions
(emacs)Regexps. Support for ERE, Extended Regular Expressions is
1 not available, but will be added in GNU M4 2.0.
1
1 If REPLACEMENT is omitted, 'regexp' expands to the index of the
1 first match of REGEXP in STRING. If REGEXP does not match anywhere
1 in STRING, it expands to -1.
1
1 If REPLACEMENT is supplied, and there was a match, 'regexp' changes
1 the expansion to this argument, with '\N' substituted by the text
1 matched by the Nth parenthesized sub-expression of REGEXP, up to
1 nine sub-expressions. The escape '\&' is replaced by the text of
1 the entire regular expression matched. For all other characters,
1 '\' treats the next character literally. A warning is issued if
1 there were fewer sub-expressions than the '\N' requested, or if
1 there is a trailing '\'. If there was no match, 'regexp' expands
1 to the empty string.
1
1 The macro 'regexp' is recognized only with parameters.
1
1 regexp(`GNUs not Unix', `\<[a-z]\w+')
1 =>5
1 regexp(`GNUs not Unix', `\<Q\w*')
1 =>-1
1 regexp(`GNUs not Unix', `\w\(\w+\)$', `*** \& *** \1 ***')
1 =>*** Unix *** nix ***
1 regexp(`GNUs not Unix', `\<Q\w*', `*** \& *** \1 ***')
1 =>
1
1 Here are some more examples on the handling of backslash:
1
1 regexp(`abc', `\(b\)', `\\\10\a')
1 =>\b0a
1 regexp(`abc', `b', `\1\')
1 error->m4:stdin:2: Warning: sub-expression 1 not present
1 error->m4:stdin:2: Warning: trailing \ ignored in replacement
1 =>
1 regexp(`abc', `\(\(d\)?\)\(c\)', `\1\2\3\4\5\6')
1 error->m4:stdin:3: Warning: sub-expression 4 not present
1 error->m4:stdin:3: Warning: sub-expression 5 not present
1 error->m4:stdin:3: Warning: sub-expression 6 not present
1 =>c
1
1 Omitting REGEXP evokes a warning, but still produces output; contrast
1 this with an empty REGEXP argument.
1
1 regexp(`abc')
1 error->m4:stdin:1: Warning: too few arguments to builtin `regexp'
1 =>0
1 regexp(`abc', `')
1 =>0
1 regexp(`abc', `', `\\def')
1 =>\def
1