如何制作一个只有n,m字符的单词的正则表达式
如果我在 file
中有以下内容:
watermelon
banana
apple
pineapple
apppppple
如何构造正则表达式,以便它只显示最少 2 个、最多 4 个连续“p”字符的单词?我尝试过
egrep 'p{2,4}' file
,但这包括我不想要的apppppple。
[root@localhost ~]# egrep 'p{2,4}' file
apple
pineapple
apppppple
If I have the following contents in file
:
watermelon
banana
apple
pineapple
apppppple
How do I construct the regex so that it only shows me words where there is a minimum of 2 and a maximum of 4 consecutive 'p' characters? I tried
egrep 'p{2,4}' file
but this includes apppppple, which I do not want.
[root@localhost ~]# egrep 'p{2,4}' file
apple
pineapple
apppppple
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
([^p]|^)
部分匹配除p
以外的任何字符或字符串开头,以及([^p]|$)
匹配除p
或字符串结尾之外的任何字符。(? 模式匹配前面或后面都没有 < 的两个、三个或四个
p
代码>p。请参阅在线演示:
输出:
You can use
The
([^p]|^)
part matches any char other thanp
or start of string, and([^p]|$)
matches any char other thanp
or end of string.The
(?<!p)p{2,4}(?!p)
pattern matches two, three or fourp
s that are not preceded nor followed withp
.See the online demo:
Output: