带有重复次数的 awk 正则表达式语法 - gawk 3 和 gawk 4 之间的不同处理

use*_*222 6 awk posix regular-expression

我无法理解或解释为什么以下表达式失败gawk 3.1.x但在 gawk 中有效4.1.x

(最小工作示例)

echo ";#ADCDE#" | awk '/#.{5}#$/'-> 在 中产生匹配,在gawk 4.1.x中不产生匹配gawk 3.1.x

echo ";#ADCDE#" | awk '/#.*#$/' -> 在两者中产生匹配

在如何处理正则表达式方面,gawk 3 和 4 之间有什么变化吗?我不认为重复构造 {n} 对正则表达式来说是新的。如果我使用字符类或 [AZ] 更改点 (.),则会发生相同的行为

X T*_*ian 11

--posix在 3.1 中添加作品

echo ";#ADCDE#" | gawk --posix '/#.{5}#$/'
Run Code Online (Sandbox Code Playgroud)

我有

awk --version
GNU Awk 3.1.6
...
Run Code Online (Sandbox Code Playgroud)

从我的man awk页面

   r{n}
   r{n,}
   r{n,m}     One  or two numbers inside braces denote an interval expres?
              sion.  If there is one number in the braces,  the  preceding
              regular  expression r is repeated n times.  If there are two
              numbers separated by a comma, r is repeated n  to  m  times.
              If  there  is  one  number  followed  by  a comma, then r is
              repeated at least n times.
              Interval expressions are only available if either --posix or
              --re-interval is specified on the command line.
Run Code Online (Sandbox Code Playgroud)

  • 我在 gawk 4.0.0 更新日志中发现了这条评论:`8. 间隔表达式现在是 GNU Awk 语法的默认正则表达式的一部分。` [link](https://lists.gnu.org/archive/html/info-gnu/2011-06/msg00013.html) 我想这就解决了. (2认同)