检索以EOL,Space结尾或没有任何字符的单词。正则表达式

时间:2012-05-29 00:56:14

标签: regex

我有一个包含这些模式的文件

word word2
word
word word
word wordword

我需要计算所有只是'word'而不是'word2'或wordword的单词。

我试过

$ grep 'word[^a-ZA-Z0-9 | $]' testWordCount.txt       
$ grep 'word[^a-ZA-Z0-9]' testWordCount.txt    
$ grep 'word[$| ]' testWordCount.txt

很抱歉,如果其中一些没有意义。我正在学习正则表达式。很抱歉不包括用于正则表达式的工具。

2 个答案:

答案 0 :(得分:2)

使用以下正则表达式匹配行:

/\bword\b/

\bword boundary anchor,它会匹配单词的开头,单词的结尾,行的开头或行的结尾。

您可以在RegexPal

测试此表达式

我看到你正在使用grep - 这个正则表达式引擎使用\<\>转义为字边界。

/\<word\>/

此外,这里是你如何计算bash中的所有实例:

cat testWordCount.txt | tr ' ' '\n' | grep -c '\<word\>'

答案 1 :(得分:0)

egrep -o在一行上打印匹配的标记,最后可以轻松计算。 \b表示边界或类似内容。

egrep -o "\bword\b" words.txt | wc