2

I use the following pattern for regex match in grep (zgrep, as searching within compressed files) as

zgrep -P '(?<=start).{20,120}(?=end)' *

I need to add some terms with OR operator in the middle, but this pattern does not work

zgrep -P '(?<=start)[first|second|third].{20,120}(?=end)' *

How can I introduce first|second|third in the middle of the matching string?

Googlebot
  • 1,959
  • 3
  • 26
  • 41
  • @jimmij wonderful. So silly of me. If you post it as an answer, I can accept it. Maybe helping someone else, as I couldn't find by searching. – Googlebot Feb 09 '19 at 00:57

1 Answers1

3

Expression in brackets [..] matches any single character from the list, so [first|second|third] matches each letter separately: f, i, r, s, t, |, e, etc. (notice no need of second s). There are few exceptions from that rule like ^ at the beginning is negation or - marks character range, but the pipe | is not special so grep just searches for this sign in the file.

What you want is (first|second|third). Everything inside (..) forms a sub-expression of main regular expression and can be combined/joined with | which is interpreted as OR operator.

jimmij
  • 47,140