2

This question already has an answer here:

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?

marked as duplicate by Jeff Schaller, Thomas, telcoM, Christopher, αғsнιη Feb 10 at 10:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @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 9 at 0:57
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.