我有一个关于正则表达式的问题,那就是{n}和{n, m}的最大重复次数。
$ man grep
...
Repetition
A regular expression may be followed by one of several repetition operators:
...
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times. This is a GNU extension.
{n,m} The preceding item is matched at least n times, but not more than m times.
...
现在考虑一个测试文件:
$ cat ./sample.txt
1
12
123
1234
然后使用grep命令查找重复出现了2次的数字[0-9]:
$ grep "[0-9]\{2\}" ./sample.txt
12
123
1234
为什么这个包括123和1234?
另外,我在相同的文本文件中使用grep查找至少重复2次但不超过3次的数字:
$ grep "[0-9]\{2,3\}" ./sample.txt
12
123
1234
为什么这会返回“1234”?
一个明显的解决方法是使用grep和反向grep来过滤掉过多的结果。例如,
$ grep "[0-9]\{2,\}" ./sample.txt | grep -v "[0-9]\{4,\}"
12
123
有人能帮我理解为什么{n}会返回包含重复n次的模式的行吗?而{n,m}为什么会返回重复m次的模式?