GNU M4:定义一个匹配文本的规则,并对匹配的文本进行操作?

GNU M4: Define a rule that matches text, and operates on that matched text?

假设我有:

File:

[x]

我想定义 m4 宏:

define(`\[.*\]`, ...)

问题:这可能吗?如何做到?

这是不可能的,正如您在 manual of m4 中看到的那样:

3.1 Macro names

A name is any sequence of letters, digits, and the character ‘_’ (underscore), where the first character is not a digit. m4 will use the longest such sequence found in the input. If a name has a macro definition, it will be subject to macro expansion (see Macros). Names are case-sensitive.

Examples of legal names are: ‘foo’, ‘_tmp’, and ‘name01’.

[] 字符在宏定义中不合法。

如果你喜欢冒险,也许你可以看看 GNU m4 1.4.18 的信息页面中提到的这个实验性功能:

   An experimental feature, which would improve 'm4' usefulness, allows
for changing the syntax for what is a "word" in 'm4'.  You should use:
     ./configure --enable-changeword
if you want this feature compiled in.  The current implementation slows
down 'm4' considerably and is hardly acceptable.  In the future, 'm4'
2.0 will come with a different set of new features that provide similar
capabilities, but without the inefficiencies, so changeword will go away
and _you should not count on it_.