如何匹配以數字開頭和結尾且由 1 到 6 個字元組成的行?

如何匹配以數字開頭和結尾且由 1 到 6 個字元組成的行?

我有以下文件:

$ cat numbers
a1
12
12345
123456
19816282
1@$%6

我正在使用grep "^[0-9]\{1,6\}$" numbers它給我以下結果:

12
12345

我也很期待1@$%6結果。

答案1

結果其實是

12
12345
123456

你的正規表示式意味著“由 1 到 6 位數字組成的行,不含其他內容「所以1@$%6不匹配。

答案2

> grep "[0-9]\{1,6\}" numbers.txt
1
12
12345
123456
19816282
1@$%6

相關內容