【问题标题】:POSIX Regex: Exclude a specified word in the list using RegexPOSIX Regex:使用 Regex 排除列表中的指定单词
【发布时间】:2017-10-02 06:04:37
【问题描述】:

我有以下单词列表

www.home.example.com
www.example.com
home.example.com
google.com
example.com
child.example.com
sameer.example.com
sameer.google.com

我需要一个只匹配child domains of example.com 的正则表达式,这意味着我需要以下答案:

www.home.example.com
home.example.com
child.example.com
sameer.example.com

但不使用egrep -v 选项。

我尝试了egrep -i '(([(a-zA-Z0-9\-\.|^www)]*)\.example\.com)',但没有成功。任何帮助将不胜感激。

【问题讨论】:

  • 试试grep -v '^red$'
  • @WiktorStribiżew,看起来这很有效,请将其作为答案,以便我支持您的答案。
  • @WiktorStribiżew,但有没有办法可以跳过使用-v?因为不确定我的设备是否支持 -v。我可以使用!之类的东西吗
  • 那么我需要知道这是否是实际数据。您真的排除了red 行吗?还是只是一个“占位符”,真实数据更复杂?

标签: regex linux grep regex-negation linux-mint


【解决方案1】:

grep 方法(使用 PCRE):

grep -Pi '(?<!www)\.example\.com' file

没有 PCRE:

cat file | grep -Ei '\.example\.com' | grep -Ev '^w{3}\.example\.com'

输出:

www.home.example.com
home.example.com
child.example.com
sameer.example.com

【讨论】:

  • 我的设备使用 POSIX,我的设备不支持 PCRE,我使用的是自定义内核。
  • @WiktorStribiżew,在这种情况下不使用 -v 只是一时兴起,我想
【解决方案2】:

我不确定您要排除什么,因为您说您只想匹配给定域中的那些。如果单词在文件中的不同行中,不应该将匹配锁定到行尾吗?

grep -e '\.example\.com$' domains 

如果您想在此之后排除某些内容,那么无论如何都使用grep -vit's part of POSIX

【讨论】:

  • 他想排除www.example.com
  • @RomanPerekhrest,是的,示例输出缺少该内容。但问题并没有说明排除它的规则是什么。
猜你喜欢
  • 2018-08-24
  • 2018-01-15
  • 2013-10-29
  • 1970-01-01
  • 2014-07-21
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多