2

我难住了!试图编写一个 awk 正则表达式来匹配一个字符串与 11 位数字。

我试过了:

if (var ~ /^[0-9]{11}$/ )
if (var ~ /^([0-9]){11}$/ )
if (var ~ /^([0-9]{11})$/ )
if (var ~ /^[0-9]{11}/ ) # altho I really do need to check the whole str
if (var ~ /[0-9]{11}/ )

如果我用这个......

if (var ~ /^[0-9]+/ ) 

然后我得到一个匹配 - 但我需要检查 11 位数字。

4

3 回答 3

3

You described your problem, but didn't tell us your awk version. It is an important information.

but this may work for your case:

if (var ~ /^[0-9]+$/ && length(var)==11)

If we know the version, there could be simpler solution.

于 2013-04-18T17:27:23.547 回答
1

如果您试图匹配字符串中某处的11连续数字:

使用测试文件:

hi12345678910
hi1234

windows版awk命令行:

awk --posix "{ if ($1 ~ /[0-9]{11}/) print}" testfile.txt

它打印:

hi12345678910
于 2013-04-18T16:31:38.533 回答
0

Doh - 忘记了 RTM:

              Interval expressions are only available if either --posix or
              --re-interval is specified on the command line.
于 2013-04-18T16:30:12.947 回答