regex - 匹配不必要的连续出现 [英] regex - matching non-necessarily consecutive occurrences
问题描述
我一直在尝试匹配 3 到 5 之间出现的 0
所以我的目标是匹配所有包含 3 到 5 个 0 的字符串.
到目前为止,我有,
egrep '[0]{3,5}' *.txt
<块引用><块引用>
预期输出
20001 [有效]
200134 [无效]
20103040 [有效]
203004038002 [无效]
但这将输出只有连续零的字符串.
如何修改代码,使其也匹配不必要的连续零?
您正在寻找的正则表达式是:
^(?!(?:.*?0){6,})(?=(?:.*?0){3,})[0-9]+$
输入文件:
cat 文件.txt2000120013420103040203004038002
命令:
要使用正则表达式,我使用 grep -P
,因为 egrep
(?!
grep -P '^(?!(?:.*?0){6,})(?=(?:.*?0){3,})[0-9]+$' 文件.txt2000120103040
说明: 首先,我使用否定前瞻来确保您不能在字符串的任何位置键入超过六个字符的 0
.之后,我使用正向前瞻来确保字符串必须包含至少 3 个 0
字符.
^
是字符串的开始.而 $
是字符串的结尾.
I have been trying to match the occurrences of 0s between the range 3 to 5
So my goal is to match all strings that contain 3 to 5 0s.
So far I have,
egrep '[0]{3,5}' *.txt
expected output
20001 [valid]
200134 [invalid]
20103040 [valid]
203004038002 [invalid]
but this would output strings that only have the consecutive zeroes.
How can I modify the code so that it would also match for non-necessarily consecutive zeroes?
The regex you're looking for is:
^(?!(?:.*?0){6,})(?=(?:.*?0){3,})[0-9]+$
Input file:
cat file.txt
20001
200134
20103040
203004038002
Command:
To use the regex I use grep -P
, because the lookaround notation (?!
is not supported in egrep
grep -P '^(?!(?:.*?0){6,})(?=(?:.*?0){3,})[0-9]+$' file.txt
20001
20103040
Explanation: First I use a negative lookahead to make sure you can't type more than six characters of 0
anywhere in the string. After that I use a positive lookahead to make sure that the string must contain at least 3 characters of 0
.
The ^
is the start of the string. And the $
is the end of the string.
这篇关于regex - 匹配不必要的连续出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!