GAWK 的 gensub() 中参数 "replacement" 的类型是什么?
What is the type of argument "replacement" in gensub() of GAWK?
The prototype of the function gensub()
in GAWK 是
gensub(regexp, replacement, how [, target])
根据我对例子的观察,
regexp
是用斜杠括起来的正则表达式
我在示例中看到一个带引号的字符串被提供给 replacement
(参见下面的示例)。
但它可以包含对匹配子字符串中的组的反向引用(参见下面的示例),这似乎
我认为 replacement
的类型是正则表达式,并且提供给 replacement
的引用字符串被强制转换为正则表达式。
现在的我
困惑:replacement
的类型是什么,字符串还是正则
表达式?
我可以给出一个用斜杠括起来的正则表达式吗
replacement
?
例如,来自同一个 link:
$ gawk '
> BEGIN {
> a = "abc def"
> b = gensub(/(.+) (.+)/, "\2 \1", "g", a)
> print b
> }'
-| def abc
我可以用 b =
gensub(/(.+) (.+)/, / /, "g", a)
替换 b = gensub(/(.+) (.+)/, "\2 \1", "g", a)
吗?
顺便说一下,-| def abc
是什么意思?
首先,替换是具有有限元字符集的字符串。
如果使用正则表达式作为替换编译,那么它可能会被接受;我不想弄清楚它的作用。
-| def abc
大部分只是前面(说明性)命令的输出。 -|
的作用在 typographical conventions 中解释为标准输出的字形标记输出;大多数其他示例输出在输出之前都有该标记。无论如何,它不是 awk
命令的一部分。 awk
命令会生成 def abc
.
What characters are treated specially?
手册说(在 gensub()
):
This is done by using parentheses in the regexp to mark the components and then specifying ‘\N’ in the replacement text, where N is a digit from 1 to 9.
它还提到'超过 sub
和 gsub
提供),因此查看 gsub()
,它说:
As in sub()
, the characters ‘&’ and ‘\’ are special
和sub()
说:
If the special character ‘&’ appears in replacement, it stands for the precise substring that was matched by regexp. … The effect of this special character (‘&’) can be turned off by putting a backslash before it in the string. As usual, to insert one backslash in the string, you must write two backslashes. Therefore, write ‘\&’ in a string constant to include a literal ‘&’ in the replacement.
The prototype of the function gensub()
in GAWK 是
gensub(regexp, replacement, how [, target])
根据我对例子的观察,
regexp
是用斜杠括起来的正则表达式我在示例中看到一个带引号的字符串被提供给
replacement
(参见下面的示例)。但它可以包含对匹配子字符串中的组的反向引用(参见下面的示例),这似乎 我认为
replacement
的类型是正则表达式,并且提供给replacement
的引用字符串被强制转换为正则表达式。现在的我 困惑:
replacement
的类型是什么,字符串还是正则 表达式?
我可以给出一个用斜杠括起来的正则表达式吗
replacement
?例如,来自同一个 link:
$ gawk ' > BEGIN { > a = "abc def" > b = gensub(/(.+) (.+)/, "\2 \1", "g", a) > print b > }' -| def abc
我可以用
b = gensub(/(.+) (.+)/, / /, "g", a)
替换b = gensub(/(.+) (.+)/, "\2 \1", "g", a)
吗?顺便说一下,
-| def abc
是什么意思?
首先,替换是具有有限元字符集的字符串。
如果使用正则表达式作为替换编译,那么它可能会被接受;我不想弄清楚它的作用。
-| def abc
大部分只是前面(说明性)命令的输出。 -|
的作用在 typographical conventions 中解释为标准输出的字形标记输出;大多数其他示例输出在输出之前都有该标记。无论如何,它不是 awk
命令的一部分。 awk
命令会生成 def abc
.
What characters are treated specially?
手册说(在 gensub()
):
This is done by using parentheses in the regexp to mark the components and then specifying ‘\N’ in the replacement text, where N is a digit from 1 to 9.
它还提到'超过 sub
和 gsub
提供),因此查看 gsub()
,它说:
As in
sub()
, the characters ‘&’ and ‘\’ are special
和sub()
说:
If the special character ‘&’ appears in replacement, it stands for the precise substring that was matched by regexp. … The effect of this special character (‘&’) can be turned off by putting a backslash before it in the string. As usual, to insert one backslash in the string, you must write two backslashes. Therefore, write ‘\&’ in a string constant to include a literal ‘&’ in the replacement.