如何在sed正则表达式中使用\ digit

时间:2016-08-19 08:00:16

标签: sed

我读过sed信息。在“3.3正则表达式语法概述”中 有一个描述:

\digit
    Matches the digit-th \(...\) parenthesized subexpression in the regular expression.
    This is called a back reference. Subexpressions are implicitly numbered by
    counting occurrences of \( left-to-right.

我不知道这意味着什么。谁可以举个例子?

1 个答案:

答案 0 :(得分:2)

当然!

n

图形:

$ echo "23 45" | sed -r 's/^([0-9]*)/---\1---/'
---23--- 45

如您所见,在sed -r 's/^([0-9]*)/---\1---/' # ^^^^^^^^ ^^ # capture -----| # print back 表单上的sed表达式中,您可以“捕获”搜索块中的模式,然后将其打印回 replace < / em>阻止使用s/search/replace\1,...数字是连续的,对应于已捕获的第1个,第2个......组。

\2