Regex to capture a whole word only using egrep

2019-06-24 10:43发布

I am using egrep to look through scripts in our perforce NAS.

i am trying to find where we use RCP & RSH....The problem i have is that 1) I suck at REGEX; 2) i am picking up junk I am not interested in. For example if a file has the word strcpy..it picks up on RCP..or ownership..hits on RSH.

Obviously i am not interested in those, but I don't want to exclude lines based on the words ownership or strcpy...because they may be used in conjunction...and its not a complete list.

Here is my regex

 'ftp|rcp|rsh' 

How can mod these to hit on FTP, but not SFTP...rcp but no strcpy, rsh but not ownership.....etc.?

So Things I would want to match.

ftp
`ftp`
/ftp/
"PUNCT"FTP"PUNCT"

2条回答
beautiful°
2楼-- · 2019-06-24 11:26

There are several metacharacters:

  • \b word boundary
  • \< word start
  • \> word end

So, one possible regex is: \<(ftp|rcp|rsh)\>

查看更多
倾城 Initia
3楼-- · 2019-06-24 11:41

Maybe you need something like this:

\b < - this is the border of word

\bpattern\b <- this pattern will match only pattern, but not otherbigpatternthatyounotneed

查看更多
登录 后发表回答