我正在使用下载的名为“sowpods.txt”的拼字游戏单词列表,并尝试使用它grep
来查找符合以下条件的所有单词:
- 7 个字母的单词
- 以同一个字母开头和结尾
- 第二、第四和第六个字母相同
- 第 3 和第 5 个字母不同
我目前为止
grep -i "^(.).*\1$" sowpods.txt > output.txt
给出了反向引用错误,所以我尝试使用在线指南,但它们非常令人困惑。这可能吗?如果可以,有人能帮忙吗?
我在 Mac 上并使用默认终端。
答案1
-P
如果您的系统上可用,请使用选项(PCRE):
grep -P '^(?=[a-zA-Z]{7}$)(.)(?!\1)(.)(?!\1)(?!\2)(.)\2(?!\1)(?!\2)(?!\3).\2\1$' inputfile
解释:
^
(?=[a-zA-Z]{7}$) : positive lookahead, zero-length assertion that make sure we have exactly 7 letters. You may use \pL{7} if you want to deal with any laguage
(.) : first letter, captured in group 1
(?!\1) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
(.) : second letter, captured in group 2
(?!\1) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
(?!\2) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 2 after
(.) : third letter, captured in group 3
\2 : fourth letter == second letter
(?!\1) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 1 after
(?!\2) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 2 after
(?!\3) : negative lookahead, zero-length assertion that make sure we don't have the same letter as in group 3 after
. : fifth letter
\2 : sixth letter == second letter
\1 : seventh letter == first letter
$